Convert To Binary
-
I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0
-
I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0
Giorgi Moniava wrote: can you give me any link where i can find information about converting from decimal to binary I found this in my toolbox. It's old so adjust as necessary:
void DWORD_To_BinaryString( DWORD value,char string[] )
{
for (int bit = 0; bit < 32; bit++)
{
DWORD mask = 0x80000000 >> bit;if ((value & mask) == mask) string\[bit\] = '1'; else string\[bit\] = '0'; } string\[32\] = '\\0';
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
I have written class inherited from CString for doing arithmetic operations with huge numbers,but it takes too long to convert a decimal number to binary using standard algorithm(dividing on 2,...). situation is like this: i want to convert (for example) CString g = "134.......45645675"; to Binary //CString binary; //binary=SOMEFUNCTION(g); binary = "10000100...10011" can you give me any link where i can find information about converting from decimal to binary using shift left and shift right operaations,or give me any tipps,i think using these operations will make conversion faster. Thanks in advance m0n0
well, i don't know any way to convert a string with an integer long enough into another "binary" string. if you work directly on an integer, you can do this :
int iVal = 125932; // For example
CString strVal = ""; // CString because more easy to usewhile (/*...*/) {
strVal.Insert(0, ((iVal & 0x00000001) ? "1" : "0"));
iVal >>= 1;
}BUT ! if you have a string, unless you cast its content into a binary (short, int or long) type, this method is not applyable beacause of the risk of bits lack (overflow). do you already have some shorst ideas on the subject to share ? [EDIT] ...And as Mr Giorgi Moniava asked to Mr Prakash, i'll try to explain a bit more my code (which is quite similar to Mr Prakash's). - with CString::Insert(0, ...), I add at position 0 the second parameter (i hope i place them in the right order). - with (iVal & 0x00000001), I use the Bitwise-AND operation to get if the lower bit is set at 1 or 0. In fact, I should have written ((iVal & 0x00000001) == 0x00000001). this operation does this :
iVal -> 100010101101011010010100001010011 **&** 000000000000000000000000000000001 ------------------------------------- == 000000000000000000000000000000001 iVal -> 100010101101011010010100001010100 **&** 000000000000000000000000000000001 ------------------------------------- == 000000000000000000000000000000000
that's what Mr Prakash does with its
mask
. - if you know about the C/C++ operator?:
, you also know that if its 1st parameter (the condition) is true, then the 2nd parameter is returned, otherwise, it is the 3rd parameter to be returned. This way, the condition is true if the LSB (Lower Significant Bit) is set to 1. So, i insert "1" in the string. - Then, i shift all the bits to the right and test again what i've just done before... [/EDIT]
TOXCCT >>> GEII power
[toxcct][ -
Giorgi Moniava wrote: can you give me any link where i can find information about converting from decimal to binary I found this in my toolbox. It's old so adjust as necessary:
void DWORD_To_BinaryString( DWORD value,char string[] )
{
for (int bit = 0; bit < 32; bit++)
{
DWORD mask = 0x80000000 >> bit;if ((value & mask) == mask) string\[bit\] = '1'; else string\[bit\] = '0'; } string\[32\] = '\\0';
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Thanks David for your attention, but i need function like CString string_To_BinaryString( CString value ) { CString result; ... ... return result; } you know actually variable 'value' in my program appears to be huge number example : "72389472389472389....234234234234" it maybe 300 digit or more.thats why standard algorithm(division by 2,...) is very slow.you know i am a little inexperienced programmer so things like '0x80000000' are not similiar to me , i would be very thankful to you if you could give me a little explanation about how to adjust your function in order to get what i need. Thanks in advance m0n0
-
Thanks David for your attention, but i need function like CString string_To_BinaryString( CString value ) { CString result; ... ... return result; } you know actually variable 'value' in my program appears to be huge number example : "72389472389472389....234234234234" it maybe 300 digit or more.thats why standard algorithm(division by 2,...) is very slow.you know i am a little inexperienced programmer so things like '0x80000000' are not similiar to me , i would be very thankful to you if you could give me a little explanation about how to adjust your function in order to get what i need. Thanks in advance m0n0
I did a program like this before about 14 years ago. There was no limit to the length of the operands and the supported operators were +, -, *, /, ^, and !. I did not have to convert anything to binary, though.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
well, i don't know any way to convert a string with an integer long enough into another "binary" string. if you work directly on an integer, you can do this :
int iVal = 125932; // For example
CString strVal = ""; // CString because more easy to usewhile (/*...*/) {
strVal.Insert(0, ((iVal & 0x00000001) ? "1" : "0"));
iVal >>= 1;
}BUT ! if you have a string, unless you cast its content into a binary (short, int or long) type, this method is not applyable beacause of the risk of bits lack (overflow). do you already have some shorst ideas on the subject to share ? [EDIT] ...And as Mr Giorgi Moniava asked to Mr Prakash, i'll try to explain a bit more my code (which is quite similar to Mr Prakash's). - with CString::Insert(0, ...), I add at position 0 the second parameter (i hope i place them in the right order). - with (iVal & 0x00000001), I use the Bitwise-AND operation to get if the lower bit is set at 1 or 0. In fact, I should have written ((iVal & 0x00000001) == 0x00000001). this operation does this :
iVal -> 100010101101011010010100001010011 **&** 000000000000000000000000000000001 ------------------------------------- == 000000000000000000000000000000001 iVal -> 100010101101011010010100001010100 **&** 000000000000000000000000000000001 ------------------------------------- == 000000000000000000000000000000000
that's what Mr Prakash does with its
mask
. - if you know about the C/C++ operator?:
, you also know that if its 1st parameter (the condition) is true, then the 2nd parameter is returned, otherwise, it is the 3rd parameter to be returned. This way, the condition is true if the LSB (Lower Significant Bit) is set to 1. So, i insert "1" in the string. - Then, i shift all the bits to the right and test again what i've just done before... [/EDIT]
TOXCCT >>> GEII power
[toxcct][i copied this code to my program and it works ideally to my surprise ,because i am unfamiliar to things like "0x00000001" , but unfortunately i need to cenvert Strings to Binary Strings ex : "17"DEC="10001"BIN and they appear to be very long(100 or more digits), anyway i'd be very thankful to you if you could give me a little explanation on how your code works, THanks in advance m0n0
-
i copied this code to my program and it works ideally to my surprise ,because i am unfamiliar to things like "0x00000001" , but unfortunately i need to cenvert Strings to Binary Strings ex : "17"DEC="10001"BIN and they appear to be very long(100 or more digits), anyway i'd be very thankful to you if you could give me a little explanation on how your code works, THanks in advance m0n0
Giorgi Moniava wrote: anyway i'd be very thankful to you if you could give me a little explanation on how your code works, certainly.... read again my previous post. i've added some explainations about my code at its end... cheers,
TOXCCT >>> GEII power
[toxcct][VisualCalc]