字符数组到十六进制字符串C ++ [英] Char array to hex string C++

查看:184
本文介绍了字符数组到十六进制字符串C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索char *到十六进制字符串之前,但实现我发现在十六进制字符串的末尾添加一些不存在的垃圾。我收到来自套接字的数据包,我需要将其转换为十六进制字符串的日志(空终止缓冲区)。

I searched char * to hex string before but implementation i found adds some non existant garbage at the end of hex string. I receive packets from socket, and i need to convert it to hex string for log(null-terminated buffer). Can somebody advice me a good implemetation for C++?

谢谢!

推荐答案

p>假设数据是一个char *。使用std :: hex的工作示例:

Supposing data is a char*. Working example using 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();

这篇关于字符数组到十六进制字符串C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆