use of _ltoa
-
Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti
-
Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti
-
Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti
I think you want to convert a binary string into its value ? Right ? Maybe you can write a simple function to do that:
int BoolStringToVal(char* szString) { int Value = 0; for (int Index=strlen(szString)-1; Index>=0; Index--) { int BinValue = 1 << (Index - strlen(szString) + 1 ); if (szString[Index]) Value += BinValue ; } return Value; }
So, what it does is look for each char in the string and if this char is 1 then we add its corresponding value to the total value. The code has not been tested so I'm not sure if this will work, but you got the principle. -
when I check the values stored in both of the arrays, something like the following is observed ... binEffects[3] = '0' binEffects[2] = '0' binEffects[1] = '' binEffects[0] = '1' and the source array contains ... m_Effects[0] = '0' m_Effects[1] = '0' m_Effects[2] = '0' m_Effects[3] = '1' Even if i use memcmp(), it returns -1 if this example is concerned. Hence, i've a doubt regarding _ltoa function since it sets the values in binEffects. Kranti
-
when I check the values stored in both of the arrays, something like the following is observed ... binEffects[3] = '0' binEffects[2] = '0' binEffects[1] = '' binEffects[0] = '1' and the source array contains ... m_Effects[0] = '0' m_Effects[1] = '0' m_Effects[2] = '0' m_Effects[3] = '1' Even if i use memcmp(), it returns -1 if this example is concerned. Hence, i've a doubt regarding _ltoa function since it sets the values in binEffects. Kranti
Did you read my post ? That is a standard behaviour. If you want to convert an integer (let's say 3) in binary string, you won't have the zeros in front of the number, that sounds logical isn't ? So, when you convert 3 into a binary string, it's logical that you get '11' and not '0011' (how can the compiler knows that you want 2 zeros at the begining of the string). The same when you convert 1 into a string, you will get '1'.
-
Hi, I want to compare two character arrays containing binary values. For e.g. if m_Effects array contains 0001, string comparison does not return 0 even if binEffects contains binary equivalent of 1. Even if I do byte by byte comparison same problem occurs. What does _ltoa exactly perform? //--------------------------------------------------------------------- for(int i=0; i <16; i++) { _ltoa(i, binEffects, 2); if(strcmp((char *)binEffects, (char *)m_Effects) == 0) return i; } //--------------------------------------------------------------------- Thanks, Kranti
-
It would be better and faster to use the reverse function of _ltoa -> strtol. And now your code looks like that:
char* e; int iEffects = strtol((const char *)m_Effects,&e,2); for(int i=0; i <16; i++) { if(i==iEffects) return i; }
hey, it worked! :) Thanks a lot! Kranti