Blog Stats
  • Posts - 45
  • Articles - 0
  • Comments - 16
  • Trackbacks - 20

 

byte array to hex string in C++

Here is a way to convert your byte array to a hex string in c++. It will convert a byte array that looks like
2A 1F 2C
to a string value "2A1F2C"
char finalhash[20];
char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for(int j = 0; j < 10; j++){
finalhash[j*2] = hexval[((hval[j] >> 4) & 0xF)];
finalhash[(j*2) + 1] = hexval[(hval[j]) & 0x0F];
}

Feedback

# re: byte array to hex string in C++

Gravatar Hi,
I am trying to sprintf a hash value into a string .
--Code --
pHash= (LPBYTE) pCert->pCertInfo->SerialNumber.pbData;
for (i=0; i<cbdata; i++)
sprintf(SrNo,"%02X",pHash[i]);

I get only first 2 bytes in the hash, though there should be 20 butes total.

Can you suggest something> 6/3/2006 4:30 PM | Tejas

# re: byte array to hex string in C++

Gravatar You probably don't want to use sprintf.

sprintf print to the beginning of the buffer. Use strncat instead. 9/28/2006 7:15 PM | Alan

# re: byte array to hex string in C++

Gravatar Hi,

Thanks for your little code, it helped me a lot... However, i think you should change the title to "byte array to hex string in C", there is nothing about C++ here... 11/8/2006 10:39 AM | No C++ here...

# re: byte array to hex string in C++

Gravatar And by the way, the 'for' loop is not conform :
<i>
main.c:6: error: ‘for’ loop initial declaration used outside C99 mode</i> 11/8/2006 10:43 AM | No C++ here...

# re: byte array to hex string in C++

Gravatar Thanks for the code, I reqrote it using C++ & STL

std::vector<char> bFileHash (16);
infile.read(&bFileHash[0], 16);

std::string hex = "01234567890abcdef";
std::string FileHash = std::string(32, '0');
for(int pos = 0; pos < 16; pos++){
FileHash[pos*2] = hex[(bFileHash[pos] >> 4) & 0xF)];
FileHash[(pos*2) + 1] = hex[(bFileHash[pos]) & 0x0F];
}

Johannes Krampf <wuischke@amule.org> 3/13/2007 1:59 PM | Johannes Krampf

# re: byte array to hex string in C++

Gravatar How about this piece of code it prints all cleanly

DWORD TraceOut(BYTE* pConfigOut,DWORD dwSizeOfConfigOut)
{
DWORD retCode = 0;
DWORD dwSizeOfArr = (2 * dwSizeOfConfigOut) + 1;
CHAR* pszCharArr = NULL;

DWORD i = 0;

retCode = AllocateMemory(dwSizeOfArr, (PVOID*)&pszCharArr); ///you can you use malloc and initialize the array
if (retCode != ERROR_SUCCESS)
{
goto cleanUp;
}
DWORD j =0;
for(i=0; i<dwSizeOfConfigOut ; i++)
{
char ch = pConfigOut[i];

char ch0 = (ch>>4) & 0x0F;
char ch1 = ch & 0x0F;



if((0<= ch0) && (ch0 <= 9))
pszCharArr[j] = '0' + ch0;
else
pszCharArr[j] = 'A' + (ch0 - 10);
j++;
if((0<= ch1) && (ch1 <= 9))
pszCharArr[j] = '0' + ch1;
else
pszCharArr[j] = 'A' + (ch1 - 10);
j++;
}
pszCharArr[j] = '\0';

EapTrace("Binary array is %s", pszCharArr);

cleanUp:
return retCode;
} 6/29/2007 2:04 PM | Samal

# re: byte array to hex string in C++

Gravatar Interesting. Heres a real C++ version.

std::string
hexdump(const std::string& val)
{
static const char hexval[16] = { '0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f' };
unsigned max_cols = 16;
std::ostringstream buf;
std::ostringstream raw_buf;

for (std::string::const_iterator i = val.begin(), e = val.end(); i != e;)
{
raw_buf.str("");
for (unsigned col = 0; col < max_cols; ++col)
{
if (i != e)
{
buf << hexval[ ( (*i >> 4) & 0xF ) ]
<< hexval[ ( *i & 0x0F ) ]
<< ' ';
raw_buf << (isprint(*i) ? *i : '.');
++i;
}
else
{
buf << " ";
}
}
buf << " " << raw_buf.str() << "\n";
}
buf << "\n";
return buf.str();
}
3/11/2008 7:21 AM | Tom

# re: byte array to hex string in C++

Gravatar Hmm, for a geek blog, your blog doesn't handle code that well ;)

Heres a pastebin of it, whitespace should be non-collapsing :p

http://pastebin.com/f546269db 3/11/2008 7:24 AM | Tom

# re: byte array to hex string in C++

Gravatar > Interesting. Heres a real C++ version.
> .....

Many thanks!

7/22/2008 11:43 AM | Alex

# re: byte array to hex string in C++

Gravatar Short an painless :-)

void CMyClass::dumpBlock(unsigned char* ldata)
{
CString strDumpline="0000:\t",strTmp,strTmp1="";

for (int i = 0;i<sizeof(IdentBlockType);i++)
{
strTmp.Format("%0.2X ",ldata[i]);
strDumpline+=strTmp;
strTmp1+=(ldata[i]<32?".":(CString)ldata[i]);
if ((i & 15) == 15)
{
CLogger::Log(strDumpline+"|"+strTmp1+"|");
strDumpline.Format("%0.4X:\t",i+1);
strTmp1="";
}
}
}

CString CMyClass::block2hex(const unsigned char *dataBlock,unsigned int blockLength)
{
CString strResult = "", strTmp;
for(unsigned int i=0;i<blockLength;i++)
{
strTmp.Format("%02X",dataBlock[i]);
strResult+=strTmp;
}
strResult+"\0";
return strResult;
}
9/8/2008 3:24 AM | C++ Newbie

# re: byte array to hex string in C++

Gravatar //Here's another simple method:

string toString(const unsigned char* data, const int datasize)
{
string output;
char ch[3];

for(int i = 0; i < datasize; ++i)
{
sprintf_s(ch, 3, "%02x", input[i]);
output += ch;
}
return output;
} 6/8/2009 3:40 PM | Levi

# re: byte array to hex string in C++

Gravatar Unfortunately the examples above are either written in non-standard C++ (MS only), or are just plain wrong and don't compile. Save your time - just another 3rd world-quality blog. 11/14/2009 10:23 PM | Lola

Post a comment





 

 

 

Copyright © Rishi Pande