具有十六进制负值的sscanf [英] sscanf with hexadecimal negative value
本文介绍了具有十六进制负值的sscanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将十六进制4位数值转换为十进制数,所以我使用了ssscanf,但它不适用于负数...
例如,
int test;
sscanf(0xfff6,%x,& test);
返回65526而不是-10。
如何解决这个问题?
I need to convert hexadecimal 4-digits values to decimal so I used ssscanf but it is not work on negative numbers...
For example,
int test;
sscanf("0xfff6","%x",&test);
return 65526 instead of -10.
How can I resolve that ?
推荐答案
您必须手动完成。 x
转换说明符执行从 unsigned int
的转换,并且它需要一个类型指针参数为 unsigned int
。例如对于一个强制转换:
You have to do it manually. x
conversion specifier performs a conversion from an unsigned int
and it requires an argument of type pointer to unsigned int
. For example with a cast:
unsigned int test;
int result;
sscanf("0xfff6","%x", &test);
result = (int16_t) test;
这篇关于具有十六进制负值的sscanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文