转换十六进制字符串(CHAR [])为int? [英] Convert hex string (char []) to int?
问题描述
我有一个char []包含一个值,如0x1800785,但我想给需要一个int值的功能,我如何才能将其转换为一个int?我已经搜索周围却找不到答案。谢谢你。
I have a char[] that contains a value such as "0x1800785" but the function I want to give the value to requires an int, how can I convert this to an int? I have searched around but cannot find an answer. Thanks.
推荐答案
你试过与strtol()
?
<一个href=\"http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html\">http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html
例如:
const char *hexstring = "abcdef0";
int number = (int)strtol(hexstring, NULL, 16);
在情况下,字符串再数presentation始于 0X
preFIX一必须应使用0作为基地:
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,但我不会推荐interoducing冗余。)
(It's as well possible to specify an explicit base such as 16, but I wouldn't recommend interoducing redundancy.)
这篇关于转换十六进制字符串(CHAR [])为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!