[英]Char array to hex string C++
I searched char*
to hex
string before but implementation I found adds some non-existent garbage at the end of hex
string. 我在char*
之前搜索了hex
字符串,但是发现实现在hex
字符串的末尾添加了一些不存在的垃圾。 I receive packets from socket, and I need to convert them to hex
strings for log (null-terminated buffer). 我收到了来自套接字的数据包,我需要将它们转换为hex
字符串以记录日志(以空终止的缓冲区)。 Can somebody advise me a good implementation for C++
? 有人可以建议我C++
的良好实现吗?
Thanks! 谢谢!
Supposing data is a char*. 假设数据为char *。 Working example using std::hex: 使用std :: hex的工作示例:
for(int i=0; i<data_length; ++i)
std::cout << std::hex << (int)data[i];
Or if you want to keep it all in a string: 或者,如果您想将所有内容都保留在字符串中:
std::stringstream ss;
for(int i=0; i<data_length; ++i)
ss << std::hex << (int)data[i];
std::string mystr = ss.str();
Here is something: 这里是一些东西:
char const hex_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for( int i = data; i < data_length; ++i )
{
char const byte = data[i];
string += hex_chars[ ( byte & 0xF0 ) >> 4 ];
string += hex_chars[ ( byte & 0x0F ) >> 0 ];
}
The simplest: 最简单的:
int main()
{
const char* str = "hello";
for (const char* p = str; *p; ++p)
{
printf("%02x", *p);
}
printf("\n");
return 0;
}
Code snippet above provides incorrect byte order in string, so I fixed it a bit. 上面的代码段在字符串中提供了错误的字节顺序,因此我对其进行了一些修复。
char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B','C','D','E','F'};
std::string byte_2_str(char* bytes, int size) {
std::string str;
for (int i = 0; i < size; ++i) {
const char ch = bytes[i];
str.append(&hex[(ch & 0xF0) >> 4], 1);
str.append(&hex[ch & 0xF], 1);
}
return str;
}
I've found good example here Display-char-as-Hexadecimal-String-in-C++ : 我在这里找到了一个很好的例子Display-char-as-Hexadecimal-String-in-C ++ :
std::vector<char> randomBytes(n);
file.read(&randomBytes[0], n);
// Displaying bytes: method 1
// --------------------------
for (auto& el : randomBytes)
std::cout << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned int)el);
std::cout << '\n';
// Displaying bytes: method 2
// --------------------------
for (auto& el : randomBytes)
printf("%02hhx", el);
std::cout << '\n';
return 0;
Method 1 as shown above is probably the more C++ way: 上面显示的方法1可能是更C ++的方式:
Cast to an unsigned int 转换为无符号整数
Usestd::hex
to represent the value as hexadecimal digits 使用std::hex
将值表示为十六进制数字
Usestd::setw
andstd::setfill
from<iomanip>
to format 使用<iomanip>
std::setw
和std::setfill
进行格式化
Note that you need to mask the cast int against0xff
to display the least significant byte: 请注意,您需要针对0xff
屏蔽0xff
以显示最低有效字节:
(0xff & (unsigned int)el)
.(0xff & (unsigned int)el)
。Otherwise, if the highest bit is set the cast will result in the three most significant bytes being set to
ff
. 否则,如果设置了最高位,则强制转换将导致将三个最高有效字节设置为ff
。
Using boost: 使用升压:
#include <boost/algorithm/hex.hpp>
std::string s("tralalalala");
std::string result;
boost::algorithm::hex(s.begin(), s.end(), std::back_inserter(result));
You can try this code for converting bytes from packet to a null-terminated string and store to "string" variable for processing. 您可以尝试使用此代码将字节从数据包转换为以空值结尾的字符串,然后存储为“ string”变量以进行处理。
const int buffer_size = 2048;
// variable for storing buffer as printable HEX string
char data[buffer_size*2];
// receive message from socket
int ret = recvfrom(sock, buffer, sizeofbuffer, 0, reinterpret_cast<SOCKADDR *>(&from), &size);
// bytes converting cycle
for (int i=0,j=0; i<ret; i++,j+=2){
char res[2];
itoa((buffer[i] & 0xFF), res, 16);
if (res[1] == 0) {
data[j] = 0x30; data[j+1] = res[0];
}else {
data[j] = res[0]; data[j + 1] = res[1];
}
}
// Null-Terminating the string with converted buffer
data[(ret * 2)] = 0;
When we send message with hex bytes 0x01020E0F, variable "data" had char array with string "01020e0f". 当我们发送带有十六进制字节0x01020E0F的消息时,变量“ data”具有字符串为“ 01020e0f”的char数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.