Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting decimal to binary 1

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
0
0
SE
Greetings all....

In my C++ class our tutor have given us a sheet with c++ questions.. one question follows:
Write a funtion that converts decimal to binary! (translated from swedish)
And this without ever explaining HOW to do it... I've tried to seek in BCB help files, in vain!!

Could someone plz help me?

Thanks in advance!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
you can use sscanf.
char chnum[]="123";
int inum;
sscanf(num,"%d",&inum); John Fill
1c.bmp


ivfmd@mail.md
 
you can use sscanf.
char chnum[]="123";
int inum;
sscanf(chnum,"%d",&inum); John Fill
1c.bmp


ivfmd@mail.md
 
Thanks, but when I tried your code the output (inum) was the same as the input (chnum), and I don't think it should!! Got any idee where I might have done something wrong? Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
Look in the sequence of characters:
char* xx="1234567890";
xx is a sequence of ten characters written in decimal. In the memory there will be
31h 32h, 33h, 34h, 35h, 36h, 37h, 38h, 39h, 30h.
using sscang you obtain a binary format for a ong int:
int x = 1234567890;
x in memory will look like 04h 96h 02h D2h, but the output of x and of xx usually will be the same. On depending of the processor theese bytes can change order.

If you want to scan bites, you can do:
int x=1234567890;
for(int i=0;i<32;)
{
cout<<int((x&int(1<<i++))==0);
}
cout<<endl;
John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for your help.

this is how I wrote it:
Code:
int dec;
cout << &quot;Enter an integer:&quot;;
cin >> dec;
for(int i=0;i<sizeof(dec);)
   cout << int( (dec&int(1<<i++)) == 0 );
If dec = 10, then the output was '1010', and I think this is what he was after...


Just one more thing. When I changed the last 'cout' to
(dec&int(1<<i++)) it gave the output 0208, and when I changed it to
(dec&int(i++)) it gave 0022, and when I changed it to
cout << int( (dec&int(i++)) == 0 ); it gave 1100
Why is there such a major difference between these four?

Thanks again!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
1. sizeof(dec) will be 4 bytes, but a byte is 8 bites. You need sizeof(dec)*8;
2.
for(int i=0;i<sizeof(dec)*8;) is much slower than
int y=sizeof(dec)*8;
for(int i=0;i<y;)
Do you see why? John Fill
1c.bmp


ivfmd@mail.md
 
I guess I see why...
&quot;for(int i=0;i<sizeof(dec)*8;) is much slower than
int y=sizeof(dec)*8;&quot;...This is true because in the first it will calculate the size of dec for every time the loop runs, in the second only once! True?

I'm still a bit confuesd, when I use 'sizeof(dec)' or in the simplest way '32' the output contains of 32 integers (1:s and 0:s)! Is it supposed to be like that?

I'm very grateful for your patience with me... not so familiar with binary numbers, and bytes / bits!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

FILE *filename;

void main(int argc, char *argv[])
{
int rec;
int x,z;
char temp1[25];
char temp2[25];

rec=0;

if(argc>2)
{
printf(&quot;Usage: BINARY [outfile]\n&quot;);
printf(&quot;Usage: BINARY <ENTER>\n&quot;);
exit(0);
}

if(argv[1])
{
fclose(filename);
filename=fopen(argv[1],&quot;w&quot;);
if(filename!=NULL) rec=1;
if(rec==1) fprintf(filename,&quot;\nBeavis rules!\n&quot;);
}

clrscr();

for(z = 1; z < 101; z++)
{
if(z<10)
{ printf( &quot;\n DEC: 00%d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: 00%d |&quot;, z);
goto BHEAD;
}
else if (z<=99 && z>9)
{ printf( &quot;\n DEC: 0%d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: 0%d |&quot;, z);
goto BHEAD;
}
else
{ printf ( &quot;\n DEC: %d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: %d |&quot;, z);
goto BHEAD;
BHEAD:
printf(&quot; HEX: %02X |&quot;, z); if(rec==1) fprintf(filename,&quot; HEX: %02X |&quot;, z);
printf( &quot; BIN: &quot; ); if(rec==1) fprintf(filename,&quot; BIN: &quot;);}

strcpy (temp1,&quot;&quot;);
strcpy (temp2,&quot;&quot;);

for(x=15; x> 7; x--)
for( x=7; x> -1; x--)
{
printf( &quot;%i&quot;, (z & 1 << x) > 0 ? 1 :0);
sprintf(temp1,&quot;%i&quot;, (z & 1 << x) > 0 ? 1 :0);
strcat(temp2,temp1);
}
strcat(temp2,&quot;\n&quot;); //remove spaces btwn lines in outfile
printf( &quot;\n&quot; ); //removes spaces onscreen
if(rec==1) fprintf(filename,temp2);
}
if(rec==1) fprintf(filename,&quot;\r\nThank you for flying Butthead Airlines!\n&quot;);
if(rec==1) fclose(filename);
}


NB: Accidently posted this in wrong thread, pardon my ignorance, it was 5am :)

 
thanks HsiaLin...
But I get this weirdo error message saying:
Unresolved external '__InitVCL' referenced from C:\program files\borland\cbuilder5\lib\cp32mtil.lib|crtlvcl
When I looked it up it read:
Unresolved external symbol referenced from module

The named symbol is referenced in the given module but is not defined anywhere in the set of object files and libraries included in the link. Check to make sure the symbol is spelled correctly.
You will usually see this error from the linker for C or C++ symbols if any of the following occur:

You did not properly match a symbol’s declarations of __pascal and __cdecl types in different source files.
You have omitted the name of an .OBJ file your program needs.
You did not link in the emulation library.

If you are linking C++ code with C modules, you might have forgotten to wrap C external declarations in extern “C”.

You could also have a case mismatch between two symbols.


Got any idees what it means? Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
No need to use that exact code, just look and learn, it has what you wanted to know. Thats old plain C code, you may have to use a C compiler to get it to compile. I havent seen nor used that code in years. :)
 
the CORRECT algorithm:

int nDecimal = 10;

cout << nDecimal << &quot; in binary is: &quot;;
for(int i = (sizeof(int) * 8); i > 0; i--) {
cout << ((nDecimal >> i-1) & 0x01);
}

&quot;John's&quot; example produces botched numbers...
 
Thanks... Still I'm not so comfortable with the output!
I told my tutor this was to difficult to understand, it was to confusing so we no longer have to do that one..

thanks anyway to all of you!!
It helped me understand a bit, though much is left unquestioned!!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
Why not

binary=atoi(Ascii);

hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top