Well, that depends on what the string should look like.
If you're using C/C++, you can use the
sprintf function for that:
int PrintArrayToString(unsigned char* pArray, int iArrayLength, char* szString, int iStringLength)
{
// Check pointers
if((pArray == NULL) || (szString == NULL))
return -1;
// Check buffer sizes
if(iStringLength < (iArrayLength * 2 + 1))
return -2;
// Print String
for(int i = 0; i < iArrayLength; i++)
sprintf(szString, "%s%X", szString, pArray[i]);
// Okay
return 0;
}
Example:
If your Array is :={222, 173, 192, 222}, then
PrintArrayToString returns "DEADC0DE"
Note: 222 = 0xDE ; 173 = 0xAD ; 192 = 0xC0However, there is also another way, which is a bit more complicated:
int PAS(unsigned char* pArray, int iArrayLength, char* szString, int iStringLength)
{
// Check pointers
if((pArray == NULL) || (szString == NULL))
return -1;
// Check buffer sizes
if(iStringLength < (iArrayLength * 2 + 1))
return -2;
// Temporary variable
int iChar = 0;
// Print String
for(int i = 0; i < iArrayLength; i++)
{
iChar = int((pArray[i] >> 4) & 0xF);
szString[2 * i] = char((iChar < 0xA ? 0x30 : 0x37) + iChar);
iChar = int(pArray[i] & 0xF);
szString[(2 * i) + 1] = char((iChar < 0xA ? 0x30 : 0x37) + iChar);
}
// Add NULLCHAR
szString[(iArrayLength * 2) + 1] = 0;
// Okay
return 0;
}
This function looks a little weird, however, produces the same output as the one above.
Happy Coding!
- Alhexx
- edit -
Basically, my first function does exactly the same what your code does; I was using pure C while you used objects