c++ - 我如何在 C++ 中将十六进制值解码为 ASCII

标签 c++ string hex decode

<分区>

这不是一个重复的问题,因为另一个问题是询问如何将十六进制转换为字符串,而不是对其进行解码。

我想将十六进制值转换为 ASCII 字符。我想做的和你在这个网站上做的完全一样:http://string-functions.com/hex-string.aspx 如果你输入 0000005B,它会输出 [

我这里的代码将返回一个十六进制值。假设它返回 0000005B,我想读取 [ 作为 LPCWSTR。

char mod_tostring(int state, int index, int size) {
int stringAddress = lua_tolstring(state, index, 0);
const char* const Base = (const char* const)stringAddress;
return Base[0]; };

Base[0] 是十六进制值。例如,Base[0] 可能是 0000005B。

最佳答案

我正在为您提供将十六进制转换为 ASCII 字符值的程序。您可以根据需要进行定制。

#include <stdio.h>
#include <math.h>
#include <string.h>
char conv(char *s)
{
    int l=strlen(s),i,v=0,ans=0,p;
    for(i=l-1;i>=0;i--)
    {
        if(s[i]>='A')
            p=s[i]-'A'+10;
        else
            p=s[i]-48;
        ans+=p*pow(16,v++);         
    }
    return (char)ans;
}
int main()
{
    printf("%c",conv("0000005B"));
}

关于c++ - 我如何在 C++ 中将十六进制值解码为 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43302256/

相关文章:

regex - Powershell - 拆分字符串

c# - 从字符串计算 Modbus RTU 的 CRC

c++ - 编译器忽略从函数返回本地 char* 的代码

c++ - Sonarqube 文件级别的手动测量

c++ - 执行跳过 getline()

python - python中的md5十六进制到二进制

c - 如何在 C 编程中循环遍历字节数组并将其存储为十六进制字符串

c++ - 用c++组织代码

python - 如果这个字符串有 "and ' ,我该如何读取它? Python

python - 关于python中字符串实例唯一性的一个问题