将无符号字符 * 转换为十六进制字符串
Converting unsigned char * to hexstring
下面的代码需要一个十六进制字符串(每个字节都表示为其相应的十六进制值( 将其转换为无符号字符 * 缓冲区,然后转换回十六进制字符串。 此代码正在测试从无符号 char* 缓冲区到十六进制字符串的转换 我需要通过网络将其发送到接收器进程。 我选择了十六进制字符串,因为无符号字符可以在 0 到 255 的范围内,并且在 127 之后没有可打印的字符。 下面的代码只是告诉让我烦恼的部分。它在评论中。
#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.
>C++20 方式:
unsigned char* data = new unsigned char[]{ "Hello worldntr " };
std::size_t data_size = sizeof("Hello worldntr ") - 1;
auto sp = std::span(data, data_size );
std::transform( sp.begin(), sp.end(),
std::ostream_iterator<std::string>(std::cout),
[](unsigned char c) -> std::string {
return std::format("{:02X}", int(c));
});
或者,如果要将结果存储到字符串中:
std::string result{};
result.reserve(size * 2 + 1);
std::transform( sp.begin(), sp.end(),
std::back_inserter(result),
[](unsigned char c) -> std::string {
return std::format("{:02X}", int(c));
});
Output:
48656C6C6F20776F726C640A090D00
unsigned char
的输出就像char
的输出,显然不是OP所期望的。
我在大肠杆菌上测试了以下内容:
#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<<()
引起的。我看了 cpp偏好
- 运算符<<(std::basic_ostream(和
- std::basic_ostream::运算符<<
并发现
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
unsigned char ch );
在前者中(在 M.M 的一点帮助下(。
OP 建议了一个修复程序:按位 并且似乎有效的0xff
。在 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
真的,这似乎有效。为什么?
0xff
是一个int
常量(严格地说:整数文字(,类型为int
。因此,按位 And 也提升(unsigned char)0xc0
int
,产生类型int
的结果,因此,int
的std::ostream::operator<<
被应用。
这是解决此问题的一种选择。我可以提供另一个 - 只需将unsigned char
转换为unsigned
.
如果将unsigned char
提升为int
引入了可能的符号位扩展(在这种情况下是不需要的(,则当unsigned char
转换为unsigned
时不会发生这种情况。unsigned
的输出流运算符也提供预期的输出:
#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
科里鲁的现场演示
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何在C++中将十六进制字符串转换为文本数据
- 定义宏以将前缀 0x 添加到十六进制字符串文本
- 将字符串(可以是十进制字符串或十六进制字符串)转换为整数C++
- 将 std::string( "x3BxDEx7C" ) 转换为可读的十六进制字符串?
- C++ 将十六进制字符串表示形式转换为十六进制字符串表示形式
- 字符数组到十六进制字符串的转换 - 意外输出
- C++十进制到十六进制(字符串下标超出范围)
- C++十六进制字符串转换为十六进制的字符 *
- 如何在C++中将十六进制字符串转换为字节字符串?
- 从十六进制字符串转换为十六进制字符数组
- 以C++格式将十六进制字符串转换为十进制数
- 将无符号字符 * 转换为十六进制字符串
- 在C++中将 HWND 转换为十六进制字符串
- 按位函数 AND、OR 和 XOR 在 C++ 中使用十六进制字符串
- 如何将十六进制字符串转换为 HTML 实体
- 查找十六进制字符串是 utf-8 或 utf-16
- 在 c++ 中使用字符串流将 4 个字符的十六进制字符串转换为有符号的短字符串
- 将 std::十六进制字符串转换为 std::array<T, 6>