将未签名的char *转换为hexstring [英] Converting unsigned char * to hexstring
问题描述
下面的代码采用一个十六进制字符串(每个字节表示为其对应的十六进制值)将其转换为无符号char *缓冲区,然后转换回十六进制字符串.此代码正在测试从无符号char *缓冲区到十六进制字符串的转换我需要通过网络将其发送到接收方进程.我选择了十六进制字符串,因为unsigned char可以在0到255的范围内,并且127之后没有可打印的字符.下面的代码仅告诉您困扰我的那部分.在评论中.
Below code takes a hex string(every byte is represented as its corresponidng hex value) converts it to unsigned char * buffer and then converts back to hex string. This code is testing the conversion from unsigned char* buffer to hex string which I need to send over the network to a receiver process. I chose hex string as unsigned char can be in range of 0 to 255 and there is no printable character after 127. The below code just tells the portion that bugs me. Its in the comment.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
// converts a hexstring to corresponding integer. i.e "c0" - > 192
int convertHexStringToInt(const string & hexString)
{
stringstream geek;
int x=0;
geek << std::hex << hexString;
geek >> x;
return x;
}
// converts a complete hexstring to unsigned char * buffer
void convertHexStringToUnsignedCharBuffer(string hexString, unsigned char*
hexBuffer)
{
int i=0;
while(hexString.length())
{
string hexStringPart = hexString.substr(0,2);
hexString = hexString.substr(2);
int hexStringOneByte = convertHexStringToInt (hexStringPart);
hexBuffer[i] = static_cast<unsigned char>((hexStringOneByte & 0xFF)) ;
i++;
}
}
int main()
{
//below hex string is a hex representation of a unsigned char * buffer.
//this is generated by an excryption algorithm in unsigned char* format
//I am converting it to hex string to make it printable for verification pupose.
//and takes the hexstring as inpuit here to test the conversion logic.
string inputHexString = "552027e33844dd7b71676b963c0b8e20";
string outputHexString;
stringstream geek;
unsigned char * hexBuffer = new unsigned char[inputHexString.length()/2];
convertHexStringToUnsignedCharBuffer(inputHexString, hexBuffer);
for (int i=0;i<inputHexString.length()/2;i++)
{
geek <<std::hex << std::setw(2) << std::setfill('0')<<(0xFF&hexBuffer[i]); // this works
//geek <<std::hex << std::setw(2) << std::setfill('0')<<(hexBuffer[i]); -- > this does not work
// I am not able to figure out why I need to do the bit wise and operation with unsigned char "0xFF&hexBuffer[i]"
// without this the conversion does not work for individual bytes having ascii values more than 127.
}
geek >> outputHexString;
cout << "input hex string: " << inputHexString<<endl;
cout << "output hex string: " << outputHexString<<endl;
if(0 == inputHexString.compare(outputHexString))
cout<<"hex encoding successful"<<endl;
else
cout<<"hex encoding failed"<<endl;
if(NULL != hexBuffer)
delete[] hexBuffer;
return 0;
}
// output
// can some one explain ? I am sure its something silly that I am missing.
推荐答案
unsigned char
的输出类似于 char
的输出,显然与OP期望.
The output of an unsigned char
is like the output of a char
which obviously does not what the OP expects.
我在coliru上测试了以下内容:
I tested the following on coliru:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned char)0xc0 << '\n';
return 0;
}
并得到:
Output of (unsigned char)0xc0: 0�
这是由 std :: ostream :: operator<<()
引起的,该代码是从可用的运算符中选择的.我看了cppreference
This is caused by the std::ostream::operator<<()
which is chosen out of the available operators. I looked on cppreference
找到
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
unsigned char ch );
在前者中(在M.M的帮助下).
in the former (with a little bit help from M.M).
OP提出了一个解决方案:按位使用 0xff
似乎可行.在coliru.com中进行检查:
The OP suggested a fix: bit-wise And with 0xff
which seemed to work. Checking this in coliru.com:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (0xff & (unsigned char)0xc0) << '\n';
return 0;
}
输出:
Output of (unsigned char)0xc0: c0
真的,这似乎可行.为什么?
Really, this seems to work. Why?
0xff
是一个 int
常量(严格来说: 整数文字 ),其类型为 int
.因此,按位And也将(unsigned char)0xc0
提升为 int
,产生类型为 int
的结果,因此,适用于 int
的 std :: ostream :: operator<<
.
0xff
is an int
constant (stricly speaking: an integer literal) and has type int
. Hence, the bit-wise And promotes (unsigned char)0xc0
to int
as well, yields the result of type int
, and hence, the std::ostream::operator<<
for int
is applied.
这是解决此问题的一种选择.我可以提供另一个-只需将 unsigned char
转换为 unsigned
.
This is an option to solve this. I can provide another one – just converting the unsigned char
to unsigned
.
在将 unsigned char
升级为 int
的地方引入了可能的符号位扩展名(在这种情况下是不希望的),而当unsigned char
会转换为 unsigned
. unsigned
的输出流运算符也提供了预期的输出:
Where the promotion of unsigned char
to int
introduces a possible sign-bit extension (which is undesired in this case), this doesn't happen when unsigned char
is converted to unsigned
. The output stream operator for unsigned
provides the intended output as well:
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Output of (unsigned char)0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned)(unsigned char)0xc0 << '\n';
const unsigned char c = 0xc0;
std::cout << "Output of unsigned char c = 0xc0: "
<< std::hex << std::setw(2) << std::setfill('0') << (unsigned)c << '\n';
return 0;
}
输出:
Output of (unsigned char)0xc0: c0
Output of unsigned char c = 0xc0: c0
这篇关于将未签名的char *转换为hexstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!