C/C++:将 char 中的十六进制值转换为整数

2022-01-12 00:00:00 c char c++

我将十六进制值存储为字符:

I have hexadecimal values stored as characters:

char A = '0';
char B = '6';
char C = 'E';

...我需要将它们转换为整数.我知道'atoi',但这仅适用于十进制编码的字符值.有没有类似的功能?

... I need them coverted to integers. I know 'atoi', but this only works for decimal coded char values. Any similar function?

推荐答案

你可以试试strtol.但是 strtol 需要一个以 0 结尾的 char *,所以:

You could try strtol. But strtol needs a 0 terminated char *, so:

long x = strtol((char[]){A, 0}, NULL, 16);

相关文章