Question

I am converting from a one byte hex value to it's ascii representation but when I try to print out the resulting string, the lengths are wrong for some reason.

Here is the code and the output I generate:

#include <math.h>
#include <string.h>

unsigned char numDigits(unsigned char aNum)
{
    unsigned char digits = 0x00;
    while (aNum)
    {
      //  printf("aNum: %d\n",aNum);
        aNum /= 0x0a;
        digits++;
    }
    if(digits == 0x00)
      return 1; // aNum was 0 but we still need to send the ascii representation of 0 so set lengeth to 1
    return digits;
}

string numToAscii(unsigned char aNum)
{
  int len = numDigits(aNum);
  printf("Num digits: %d\n", len);
  string asciiNum = new char[len];
  for(int i = 0; i < len; i++)
  {
    int nthPlace = (int)pow(10.0,(len-(i+1)));  
    int nthNum = aNum/nthPlace;
    asciiNum[i] = nthNum+0x30;
    printf("adding %c\n", asciiNum[i]);
    aNum-=nthNum*nthPlace;
  }
  return asciiNum;
}

int main ()
{
unsigned char one = 0xfd;
unsigned char two = 0x64;
unsigned char three = 0x40;
unsigned char four = 0x0f;
unsigned char five = 0x01;

string cone = numToAscii(one);
string ctwo= numToAscii(two );
string cthree = numToAscii(three );
string cfour = numToAscii(four );
string cfive= numToAscii(five );

printf("Len: %d One: %s\n", cone.length(), cone.c_str());
printf("Len: %d two : %s\n", ctwo.length(), ctwo.c_str());
printf("Len: %d three : %s\n", cthree.length(), cthree.c_str() );
printf("Len: %d four : %s\n", cfour.length(), cfour.c_str() );
printf("Len: %d five : %s\n",cfive.length(), cfive.c_str());

return 0;
}

OUTPUT:

Num digits: 3
adding 2
adding 5
adding 3
Num digits: 3
adding 1
adding 0
adding 0
Num digits: 2
adding 6
adding 4
Num digits: 2
adding 1
adding 5
Num digits: 1
adding 1
Len: 5 One: 253�)
Len: 6 two : 100�i
Len: 3 three : 64�
Len: 3 four : 15�
Len: 2 five : 1�
Was it helpful?

Solution 2

This happens because you initialize your string incorrectly. When you call

string asciiNum = new char[len];

you are initializing the string with uninitialized data, and you also create a memory leak, because you never call the delete[] on the new char[len].

Replace it with

string asciiNum(len, '0');

to fix the problem.

OTHER TIPS

This is wrong:

string asciiNum = new char[len];

When you initialise a C++ string from a C-style character array, it assumes that the array is terminated - that is, that there's a zero-valued byte to mark the end of the string. This is not the case here - you allocate some memory (filled with garbage), attempt to initialise the string from it (with undefined results, since it might not find a terminator), and then leak the memory since you've lost the only pointer to it.

Instead, you want:

string asciiNum(len, '\0');

or, if you prefer, create it empty:

string asciiNum;

and use push_back or += to append characters to it.

(This assumes that string is the class from the standard library, even though you're not including <string> or qualifying it with std::. Perhaps your library is twenty years out of date?)

Because noone else has posted it, have you tried:

int main ()
{
unsigned char one = 0xfd;
std::cout << static_cast<unsigned int>(one) << std::endl;
return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top