Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the hex dump function, but what I need is dec dump.

C
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]);
    }
}


How do I dump bytes to decimal string instead of heximal string?

Thanks in advance!
Posted
Updated 8-Nov-10 21:45pm
v3
Comments
Dalek Dave 9-Nov-10 3:45am    
Edited for Grammar and Readability.

It's quite trivial: use the sprintf and strcat functions.

C++
void DumpBytes(unsigned char* inputBytes, int inputBytesLength, char* ouputString)
{
   char tmp[5]; // 0-255 + whitespace + terminating NULL -> max 5 char

   outputString[0] = '\0';
   for(int i = 0; i < inputBytesLength; i++)
   {
      sprintf_s(tmp, "%u ", inputBytes[i]);
      strcat(outputString, tmp);
   }
}
 
Share this answer
 
v2
Comments
Dalek Dave 9-Nov-10 3:45am    
Good Call
A C++ answer, safer on output size potential problem:
#include <sstream>
#include <iterator>
std::string DumpBytes(unsigned char* inputBytes, int inputBytesLength)
{
   std::ostringstream oss;
   std::copy_n(inputBytes, inputBytesLength, std::ostream_iterator<int>(oss, " "));
   return oss.str();
}

cheers,
AR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900