0

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.

5
  • 1
    I did it, and then but the result is wrong. Nice contradiction Commented Nov 12, 2011 at 20:07
  • You are mixing decimal and binary number shifts/divisions. What is the char[] array that would match the sample, 9140? Commented Nov 12, 2011 at 20:09
  • jaja @sehe yeah i was confused when i write this :P, when i say "i did it" is when i can convert the digits to bytes and when i say "but the result is wrong" is when some of the digits was wrong converted :P Commented Nov 12, 2011 at 20:39
  • when i convert each digit of 9140 the output in the char array is: (0,0,0,9)(for 9),(0,0,0,0)(for 1 ?), (0,0,0,4)(for 4) and (0,0,0,0)(for 0) so i write each group of 4 bytes into the char array from right to left (...some empty bytes...0,0,0,9,0,0,0,0,0,0,0,4,0,0,0,0,...bytes of amount of digits). Commented Nov 12, 2011 at 20:45
  • @DiegoFernandoMurilloValenci, did you try my solution below? Commented Nov 12, 2011 at 20:49

4 Answers 4

2

I feel this is the most c++ way that probably answers your question, if I understood correctly:

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>

template <typename It>
It tochars(unsigned int i, It out)
{
    It save = out;

    do    *out++ = '0' + i%10;
    while (i/=10);

    std::reverse(save, out);
    return out;
}

int main()
{
    char buf[10];

    char* end = tochars(9140, buf);
    *end = 0; // null terminate

    std::cout << buf << std::endl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

well my friend, but that code convert the int to a char array, is like convert int to string, but i need is convert each int digit to a byte[4] and save it the array of char, because i send this array to java and convertit again to an integer.
You do ... what? Good luck with that.
1

Instead of using a double and the floor function, just use an int and the modulus operator instead.

void convertCantToByteArray4Digits(unsigned char *bufferDigits,int cant)
{
  int bufferPos=20;
  int cantAux=cant;
  int digit=0,cantDigits=0;
  while(cantAux > 0)
  {
    cout<<"VUELTA"<<endl;
    digit = cantAux % 10;
    cout<<"digit:"<<digit<<endl;
    cantAux /= 10;
    cout<<"cantAux/=10:"<<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;
    bufferPos-=4;
    cantDigits++;
  }

Comments

0

Why not use a union?

union {
  int i;
  char c[4];
};

i = 2530;
// now c is set appropriately

Or memcpy?

memcpy(bufferDigits, &cant, sizeof(int));

3 Comments

hello thanks for you reply, the union convert each digit of i into a byte[4]? the same occurs for memcpy?
-1 because the problem states: "The idea is separate each digit an convert it in a byte[4]". He wants five, byte[4] entities, one for each of the possible 4 digits and one for the count of digits.
i use union but for each digit not for the entire number and get the same result, it convert 9140 to a byte array that contain 9040, i need to convert each digit because i send this to JAVA for convert it again to integer.
0

Why so complicated? Just divide and take remainders. Here's a reentrant example to which you provide a buffer, and you get back a pointer to the beginning of the converted string:

char * to_string(unsigned int n, char * buf, unsigned int len)
{
  if (len < 1) return buf;

  buf[--len] = 0;

  if (n == 0 && len > 0) { buf[--len] = '0'; }

  while (n != 0 && len > 0) { buf[--len] = '0' + (n % 10); n /= 10; }

  return &buf[len];
}

Usage: char buf[100]; char * s = to_string(4160, buf, 100);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.