如何将十六进制值的字符串转换为字符串? [英] How to convert a string of hex values to a string?

查看:255
本文介绍了如何将十六进制值的字符串转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个像这样的字符串:

Say I have a string like:

string hex = "48656c6c6f";

每两个字符对应其ASCII值的十六进制表示,例如:

Where every two characters correspond to the hex representation of their ASCII, value, eg:

0x48 0x65 0x6c 0x6c 0x6f = "Hello"

那么如何从"48656c6c6f"中获取"hello"而不必创建查找ASCII表? atoi()显然在这里不起作用.

So how can I get "hello" from "48656c6c6f" without having to create a lookup ASCII table? atoi() obviously won't work here.

推荐答案

int len = hex.length();
std::string newString;
for(int i=0; i< len; i+=2)
{
    string byte = hex.substr(i,2);
    char chr = (char) (int)strtol(byte.c_str(), null, 16);
    newString.push_back(chr);
}

这篇关于如何将十六进制值的字符串转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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