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

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

问题描述

假设我有一个字符串:

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)
{
    std::string byte = hex.substr(i,2);
    char chr = (char) (int)strtol(byte.c_str(), null, 16);
    newString.push_back(chr);
}

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

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