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

How would I decode a hex value to ASCII in c++

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

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

我这里的代码将 return 一个十六进制值。假设它 returns 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"));
}