0

below is the hex dump function,but what i need is dec dump.

void DumpBytes(unsigned char* inputBytes,int inputBytesLength,char* ouputString)
{
    unsigned char const Hex[] = "01234567890abcdef";
    for (int i = 0; i < inputBytesLength; i++)  
    {
        unsigned char binByte = inputBytes[i];
        ouputString[2*i] = (Hex[(binByte & 0xf0) >> 4]);
        ouputString[2*i + 1] = (Hex[binByte & 0x0f]);
    }
}

so,how to dump bytes to decimal string instead of heximal string.thanks in advance!

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
vincent
  • 109
  • 1
  • 5

4 Answers4

1

Bytes neatly fit in 2 hexadecimal characters, but decimal can take upto 3 characters (0 - 255) and it's not so neat to store them in a string without spaces. You might just want to print it out directly with spaces:

for (int i = 0; i < inputBytesLength; i++)  
  cout << (int)inputBytes[i] << ' ';

If you do want to store them in a string though, it's definitely possible and is very similar to the existing code:

ouputString[3*i] = '0' + binByte / 100;
ouputString[3*i + 1] = '0' + (binByte / 10) % 10;
ouputString[3*i + 2] = '0' + binByte % 10;
casablanca
  • 69,683
  • 7
  • 133
  • 150
  • That last section should add '0' to convert digits to ASCII. Also noteworthy that C++ streams have an `` header you can include, and `std::setw(3)` will pad the next-streamed value to a width of 3, by default with spaces but this can be overriden using `std::setfill('0')`. – Tony Delroy Nov 09 '10 at 02:26
  • the following code seems doesn't work.ouputString[3*i] = binByte / 100; ouputString[3*i + 1] = (binByte / 10) % 10; ouputString[3*i + 2] = binByte % 10; – vincent Nov 09 '10 at 02:28
  • @vincent: as I mentioned above, you'll need to change each line as in: `ouputString[3*i] = binByte / 100 + '0';` to convert from a numeric digit to its character representation (e.g. ASCII char for zero has a value of 48 decimal). – Tony Delroy Nov 09 '10 at 02:30
  • some example code is similar to hex dumping,using a predefined unsigned char array,e.g.unsigned char const Dec[] = "01234567890abcdef";ouputString[2*i] = (Dec[?]); ouputString[2*i + 1] = (Dec[?]); – vincent Nov 09 '10 at 02:32
  • @Tony: Thanks, missed that part. @vincent: I've fixed the code now. – casablanca Nov 09 '10 at 02:41
0
void DumpBytes(unsigned char* inputBytes,int inputBytesLength,char* ouputString)
{
    for (int i = 0; i < inputBytesLength; i++)  
    {
      unsigned char binByte = inputBytes[i];
      ouputString[3*i + 2] = binByte % 10 +'0';
      binByte \= 10;
      ouputString[3*i + 1] = binByte % 10 +'0';
      binByte \= 10;
      ouputString[3*i] = binByte % 10 +'0';
    }
    ouputString[3*i] = 0;
}

Output string must be at least (3*inputBytesLength + 1) big

Lefteris E
  • 2,806
  • 1
  • 24
  • 23
0

instead of >> 4 and & 0x0f, use / 10 and % 10 twice.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • do you mean it? code like this...ouputString[2*i] = ((binByte/10) % 10); ouputString[2*i + 1] = (binByte % 10);but it still doesn't work,it's garbled,(i have put a '\0' char to the end of string) – vincent Nov 09 '10 at 02:39
0

I think you're looking for Base64 encoding.

This encoding/decoding algorithm is used by many major protocols such as HTTP and IMAP (email protocol) to convert binary data to something more, how to say it, transferable.
It will take your data and produce two-bytes representation of each of your given bytes, so consider that if you have space limits.

There are also nice answers about B64 such as this.

Community
  • 1
  • 1
Poni
  • 11,061
  • 25
  • 80
  • 121
  • not Base64 encoding,but RSA asymmetric encryption algorithm.the link : http://stackoverflow.com/questions/4121937/why-cant-convert-symbian-crsapublickey-to-descriptor is the other question i have raised – vincent Nov 09 '10 at 03:26