Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ascii十六进制值转换为Ascii数字_C_Hex - Fatal编程技术网

Ascii十六进制值转换为Ascii数字

Ascii十六进制值转换为Ascii数字,c,hex,C,Hex,可能重复: 我喜欢将ascii码表示的十六进制值转换为十六进制值表示的数字 例如: A5应转换为0xA5(即165) 但是A5是用ASCII表示的(我猜是类似于0x4135) 有什么想法吗? 谢谢 使用strtol long x = strtol("A5", 0, 16) // x is now 165 使用strtol long x = strtol("A5", 0, 16) // x is now 165 如果您有可用的strtol(我相信它在标准库中),那么它应该像调用strtol一

可能重复:

我喜欢将ascii码表示的十六进制值转换为十六进制值表示的数字

例如:

A5
应转换为
0xA5
(即165)

但是
A5
是用
ASCII
表示的(我猜是类似于
0x4135

有什么想法吗?
谢谢

使用
strtol

long x = strtol("A5", 0, 16)
// x is now 165

使用
strtol

long x = strtol("A5", 0, 16)
// x is now 165

如果您有可用的
strtol
(我相信它在标准库中),那么它应该像调用
strtol
一样简单,使用16作为基础

char *end
val = strtol("A5", &end, 16)

如果您有可用的
strtol
(我相信它在标准库中),那么它应该像调用
strtol
一样简单,使用16作为基础

char *end
val = strtol("A5", &end, 16)

另一种方法是使用sscanf():


另一种方法是使用sscanf():


我建议您阅读sscanf的友好手册页;请特别注意“%x”转换说明符。@jens:*scanf在这方面有点笨手笨脚,但如果它是更大的“从字符串中获取数据”的一部分,那么它是非常明智的。请先进行一些搜索,检查这里有多种方法:-)我建议您阅读sscanf的友好手册页;请特别注意“%x”转换说明符。@jens:*scanf在这方面有点笨手笨脚,但如果它是更大的“从字符串中获取数据”的一部分,则非常明智。请先进行一些搜索,检查这里有多种方法可以做到:-)