我有一个 char[] ,其中包含诸如“0x1800785”之类的值,但我想要为其提供该值的函数需要一个 int ,如何将其转换为 int ?我四处搜寻但找不到答案。谢谢。
你有没有尝试过strtol()
?
strtol - 将字符串转换为长整数 http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html
Example:
const char *hexstring = "abcdef0";
int number = (int)strtol(hexstring, NULL, 16);
In case the string representation of the number begins with a 0x
prefix, one must should use 0 as base:
const char *hexstring = "0xabcdef0";
int number = (int)strtol(hexstring, NULL, 0);
(也可以指定显式基数,例如 16,但我不建议引入冗余。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)