我有一个 C++ 函数,可以将 unsigned long 转换为 8 位十六进制字符字符串。我需要想出一个反向函数,它接受一个 8 位十六进制字符字符串并将其转换为一个无符号整数来表示它的字节。
原始 UINT -> char[8] 方法:
std::string ResultsToHex( unsigned int EncodeResults)
{
std::cout << "UINT Input ==>";
std::cout << EncodeResults;
std:cout<<"\n";
char _HexCodes[] = "0123456789ABCDEF";
unsigned int HexAccum = EncodeResults;
char tBuf[9];
tBuf[8] = '\0';
int Counter = 8;
unsigned int Mask = 0x0000000F;
char intermed;
// Start with the Least significant digit and work backwards
while( Counter-- > 0 )
{
// Get the hex digit
unsigned int tmp = HexAccum & Mask;
intermed = _HexCodes[tmp];
tBuf[Counter] = intermed;
// Next digit
HexAccum = HexAccum >> 4;
}
std::cout << "Hex char Output ==>";
std::cout << tBuf;
std::cout << "\n";
return std::string(tBuf);
}
这是我正在尝试编写的函数,它将以 char[8] 作为输入并转换为 UINT:
unsigned int ResultsUnhex( char tBuf[9])
{
unsigned int result = 0;
std::cout << "hex char Input ==>";
std::cout << tBuf;
std:cout<<"\n";
//CODE TO CONVERT 8 char (bit) hex char into unsigned int goes here......
//
// while() {}
//
///
std::cout << "UINT Output ==>";
std::cout << result;
std::cout << "\n";
return result;
}
我是位移的新手,任何帮助将不胜感激:)。