Geeks With Blogs
Rishi Pande Me, Myself and my obsessions
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];
}
Posted on Thursday, February 9, 2006 4:29 PM | Back to top


Comments on this post: byte array to hex string in C++

# re: byte array to hex string in C++
Requesting 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>
Left by Tejas on Jun 03, 2006 4:30 PM

# re: byte array to hex string in C++
Requesting Gravatar...
You probably don't want to use sprintf.

sprintf print to the beginning of the buffer. Use strncat instead.
Left by Alan on Sep 28, 2006 7:15 PM

# re: byte array to hex string in C++
Requesting 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...
Left by No C++ here... on Nov 08, 2006 10:39 AM

# re: byte array to hex string in C++
Requesting 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>
Left by No C++ here... on Nov 08, 2006 10:43 AM

# re: byte array to hex string in C++
Requesting 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>
Left by Johannes Krampf on Mar 13, 2007 1:59 PM

# re: byte array to hex string in C++
Requesting 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;
}
Left by Samal on Jun 29, 2007 2:04 PM

# re: byte array to hex string in C++
Requesting Gravatar...
> Interesting. Heres a real C++ version.
> .....

Many thanks!

Left by Alex on Jul 22, 2008 11:43 AM

# re: byte array to hex string in C++
Requesting 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;
}
Left by C++ Newbie on Sep 08, 2008 3:24 AM

# re: byte array to hex string in C++
Requesting 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;
}
Left by Levi on Jun 08, 2009 3:40 PM

# re: byte array to hex string in C++
Requesting 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.
Left by Lola on Nov 14, 2009 10:23 PM

# re: byte array to hex string in C++
Requesting 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.
Left by Linda on Apr 28, 2010 5:52 AM

# re: byte array to hex string in C++
Requesting 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();
}
Left by Thayne on Feb 22, 2011 1:42 PM

# re: byte array to hex string in C++
Requesting Gravatar...
hval[] is nowhere defined and if defined whats its value.
Left by Sanjay on Sep 05, 2011 5:52 AM

# re: byte array to hex string in C++
Requesting Gravatar...
Excellent stuff, keep it going....
Left by Thilina on Dec 27, 2011 11:19 PM

Your comment:
 (will show your gravatar)


Copyright © Rishi Pande | Powered by: GeeksWithBlogs.net