Blog Stats
  • Posts - 45
  • Articles - 0
  • Comments - 23
  • 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.
> .....

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

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

Gravatar C++ Newbie:
Thanks so much for this code, exactly what I needed! As Lola said, it does need a few small fixes, but it was still very useful.

I just used the dumpBlock method, and replaced the CLogger call with my own logging implementation.

Just 3 small fixes to get this working nicely:
1) IdentBlockType - no idea what this is, but replaced with a strlen(ldata)

2) Had to change
strTmp.Format("%0.2X ",ldata[i]);
to
strTmp.Format("%02X ",ldata[i]);

For the ATL::CString implementation, otherwise you randomly get FFFFFFda instead of da

3) After the for loop, you need to add another line to log any partial lines. If the last line is less than 16 characters, it gets lost as it isn't logged, in other words.
4/28/2010 5:52 AM | Linda

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

Gravatar try this stl solution:

#include <sstream>

std::string printHex(std::string const& data)
{
std::stringstream ss(std::ios::binary|std::ios::in);
ss << std::hex;
unsigned index(0);
while(index<data.size())
{
ss << static_cast<unsigned>(data[index++]);
}
return ss.str();
}
2/22/2011 1:42 PM | Thayne

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

Gravatar hval[] is nowhere defined and if defined whats its value. 9/5/2011 5:52 AM | Sanjay

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

Gravatar Excellent stuff, keep it going.... 12/27/2011 11:19 PM | Thilina

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Rishi Pande