Binary numbers

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
Hi i need help on converting numbers/characters into binary numbers.. is there a header file that lets me do it? or i need to do a lot of if statments.... my idea is to write a .txt file with all the information in it, like:

A 1000001

i'll just use a char variable to take care of the space, and use apstring to read the binary numbers so it comes out in one piece... and use another char variable to find the match... is that a good idea? or there's an easier way to do it

if you have a better idea, please tell me, appreciate any inputs!

ps. for the numbers, first convert them to ascii values, then do the conversion.. in case any of you guys are confused :)

~GiLtY
 

DAM

Diamond Member
Jan 10, 2000
6,102
1
76
are you writing a program, if so, most not all but most languages have a function to turn ascii to binar, and vice versa.




dam()
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Assuming you are getting binary numbers as user input, I think a char array is best used for the conversion (you might want to limit the length of the string, otherwise you'll have to deal with a BigInt class as well).

Once you get your user input and get the size of the input string, all you have to do is use a for loop to access each individual char of the string.

for (int i = 0; i < iStringSize; i++)
{
if (chInputString == '1')
// add 2^(iStringSize-i-1) to total
// You'll need to write a function for 2^n I guess
}


:)atwl
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
Adrian Tung:

for (int i = 0; i < iStringSize; i++)
{
if (chInputString == '1')
// add 2^(iStringSize-i-1) to total
// You'll need to write a function for 2^n I guess
}
-------->im a little confused about the looping above, please explain it in more detail what each means (like what does iStringSize stand for, the byte which the character has?)..

can't i use this to access each individual character?:

for (for int i=0; i <dimension; i++)
{
a[ i ];
}
then do the 2 to the istringsize-i-1 thingy...
thx for the input.... i am just a little bit confused :)


~GiLtY
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
iStringSize represents the size (length) of the string, so if you have a string &quot;10011&quot; then iStringSize should be 5.

So,

int iStringSize;
int iTotal = 0;

// calculate the loop
for (int i = 0; i < iStringSize; i++)
{
if (szString[ i ] == '1'){
iTotal += TwoToThePowerOf(iStringSize-i-1);
}
}


And the function TwoToThePowerOf is:

int TwoToThePowerOf(int iValue)
{
if (iValue == 0) return 1;

int iResult = 2;
for (int i = 0; i < iValue-1; i++) {
iResult *= 2;
}
return iResult;
}


BTW If you are using Win32 you might want to use the DWORD value instead of int, since DWORD goes all the way up to 2^32.


Hope that helps,
:)atwl
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
i think you are trying to use binary number as the user's input? im trying to convert character like &ntilde; to a binary number....

or they are done in the same way

~GiLtY
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Whoops, must have misread your question! :eek:

Anyway, this should be simple. You should first convert the char to it's integer value by casting it as an integer, e.g.

int x, char y;
x = (int) y;

If you remember binary math, you convert a decimal to binary by successive divisions by 2. Store those in a char array and you should have your result.


Hope that helps,
:)atwl