将十六进制字符串 (char []) 转换为 int? [英] Convert hex string (char []) to int?
本文介绍了将十六进制字符串 (char []) 转换为 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()
吗?
示例:
const char *hexstring = "abcdef0";
int number = (int)strtol(hexstring, NULL, 16);
如果数字的字符串表示以 0x
前缀开头,则 必须 应该使用 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,但我不建议引入冗余.)
(It's as well possible to specify an explicit base such as 16, but I wouldn't recommend introducing redundancy.)
这篇关于将十六进制字符串 (char []) 转换为 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文