/**
* Convert a binary array to a string.
*
* converts given string from 8-bit binary, to a hex string
* example: ASCII '0' (decimal 48, hex 0x30) -> "30"
* example: ASCII '1' (decimal 49, hex 0x31) -> "31"
*
*/
bool CPBDataNumericBinPackedHex::BinHexToString(const PBchar_t* pData, const int nDataLen, PBchar_t* pHexStringOut, const int iOutBufferMaxSize, int* pOutLen)
{
*pOutLen = 0;
bool bOk = true;
//hex will be twice the length of the raw binary:
if(iOutBufferMaxSize < (nDataLen*2) + 1) //+1 for NULL terminator
{
assert(false);
bOk = false;
return bOk;
}
strstream str(pHexStringOut, iOutBufferMaxSize);
int iHex = 0;
for(int i = 0; i < nDataLen; i++)
{
//high byte:
unsigned char chHigh = pData[i] & 0xF0;
chHigh = chHigh >> 4;
//note: NOT using std::hex directly, because it packs bytes - for example: 0x90 -> '9' not '90'
str << std::hex << (int)chHigh;
//low byte:
unsigned char chLow = pData[i] & 0x0F;
str << std::hex << (int)chLow;
}
str.flush();
*pOutLen = str.tellp();
pHexStringOut[str.tellp()] = '\0'; //NULL terminator
assert((*pOutLen >= 0));
return bOk;
}
//the inverse: HEX -> 8bit binary:
char* buffer = "BADBEEF123";
int iNumBytes = sizeof(buffer)/sizeof(buffer[0]);
unsigned char binaryBuffer[1024];
for (int i = 0; i < iNumBytes; i++)
{
//Our input 'buffer' is a HEX string (not in binary).
//So, we need to convert from hex string, into binary:
DWORD tmp = 0;
sscanf_s(&buffer[i*2], "%02x", &tmp, sizeof(DWORD));
binaryBuffer[i] = (unsigned char)tmp;
}
* Convert a binary array to a string.
*
* converts given string from 8-bit binary, to a hex string
* example: ASCII '0' (decimal 48, hex 0x30) -> "30"
* example: ASCII '1' (decimal 49, hex 0x31) -> "31"
*
* @return bool to indicate success*
*/
bool CPBDataNumericBinPackedHex::BinHexToString(const PBchar_t* pData, const int nDataLen, PBchar_t* pHexStringOut, const int iOutBufferMaxSize, int* pOutLen)
{
*pOutLen = 0;
bool bOk = true;
//hex will be twice the length of the raw binary:
if(iOutBufferMaxSize < (nDataLen*2) + 1) //+1 for NULL terminator
{
assert(false);
bOk = false;
return bOk;
}
strstream str(pHexStringOut, iOutBufferMaxSize);
int iHex = 0;
for(int i = 0; i < nDataLen; i++)
{
//high byte:
unsigned char chHigh = pData[i] & 0xF0;
chHigh = chHigh >> 4;
//note: NOT using std::hex directly, because it packs bytes - for example: 0x90 -> '9' not '90'
str << std::hex << (int)chHigh;
//low byte:
unsigned char chLow = pData[i] & 0x0F;
str << std::hex << (int)chLow;
}
str.flush();
*pOutLen = str.tellp();
pHexStringOut[str.tellp()] = '\0'; //NULL terminator
assert((*pOutLen >= 0));
return bOk;
}
//the inverse: HEX -> 8bit binary:
char* buffer = "BADBEEF123";
int iNumBytes = sizeof(buffer)/sizeof(buffer[0]);
unsigned char binaryBuffer[1024];
for (int i = 0; i < iNumBytes; i++)
{
//Our input 'buffer' is a HEX string (not in binary).
//So, we need to convert from hex string, into binary:
DWORD tmp = 0;
sscanf_s(&buffer[i*2], "%02x", &tmp, sizeof(DWORD));
binaryBuffer[i] = (unsigned char)tmp;
}
Comments
Post a Comment