i tried to convert the digits from a number like 9140 to a char array of bytes, i finally did it, but for some reason one of the numbers is converted wrong.
The idea is separate each digit an convert it in a byte[4] and save it a global array of bytes, that means that array have a digit each 4 positions, i insert each digit at the end of array and finally i insert the amount of digits at the end of the array.
the problem is randomly with some values, for example for the value 25 it works but for 9140 it return me 9040, which could be the problem? this is the code:
void convertCantToByteArray4Digits(unsigned char *bufferDigits,int cant){
//char bufferDigits[32];
int bufferPos=20;
double cantAux=cant;
int digit=0,cantDigits=0;
double subdigit=0;
while(cantAux > 0){
cout<<"VUELTA"<<endl;
cantAux/=10;
cout<<"cantAux/=10:"<<cantAux<<endl;
cout<<"floor"<<floor(cantAux)<<endl;
subdigit=cantAux-floor(cantAux);
cout<<"subdigit"<<subdigit<<endl;
digit=static_cast<int>(subdigit*10);
cout<<"digit:"<<subdigit*10<<endl;
cantAux=cantAux-subdigit;
cout<<"cantAux=cantAux-subdigit:"<<cantAux<<endl;
bufferDigits[bufferPos-4] = (digit >> 24) & 0xFF;
std::cout<<static_cast<int>(bufferDigits[bufferPos-4])<<std::endl;
bufferDigits[bufferPos-3] = (digit >> 16) & 0xFF;
std::cout<<static_cast<int>(bufferDigits[bufferPos-3])<<std::endl;
bufferDigits[bufferPos-2] = (digit >> 8) & 0xFF;
std::cout<<static_cast<int>(bufferDigits[bufferPos-2])<<std::endl;
bufferDigits[bufferPos-1] = (digit) & 0xFF;
std::cout<<static_cast<int>(bufferDigits[bufferPos-1])<<std::endl;
/*bufferDigits[0] = digit >> 24;
std::cout<<bufferDigits[0]<<std::endl;
bufferDigits[1] = digit >> 16;
bufferDigits[2] = digit >> 8;
bufferDigits[3] = digit;*/
bufferPos-=4;
cantDigits++;
}
cout<<"sizeof"<<sizeof(bufferDigits)<<endl;
cout<<"cantDigits"<<cantDigits<<endl;
bufferPos=24;
bufferDigits[bufferPos-4] = (cantDigits) >> 24;
//std::cout<<bufferDigits[bufferPos-4]<<std::endl;
bufferDigits[bufferPos-3] = (cantDigits) >> 16;
bufferDigits[bufferPos-2] = (cantDigits) >> 8;
bufferDigits[bufferPos-1] = (cantDigits);
}
the bufferDigits have a size of 24 bytes, the cant parameter is the number to convert, i receive any question about my code.
I did it, and thenbut the result is wrong. Nice contradiction9140?