将十六进制字符串(char [])转换为int? [英] Convert hex string (char []) to int?
本文介绍了将十六进制字符串(char [])转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个char [],其中包含一个值,如0x1800785,但我想给该值的函数需要一个int,我如何将它转换为一个int?我已经搜索了,但找不到答案。谢谢。
解决方案
您是否尝试过 strtol()
? >
示例:
const char * hexstring =abcdef0;
int number =(int)strtol(hexstring,NULL,16);
如果数字的字符串表示以 0x
前缀,一个必须应该使用0作为基础:
const char * hexstring = 0xabcdef0;
int number =(int)strtol(hexstring,NULL,0);
(也可以指定一个明确的基础,例如16,但我不建议介绍冗余。)
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.
解决方案
Have you tried strtol()
?
strtol - convert string to a long integer
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);
(It's as well possible to specify an explicit base such as 16, but I wouldn't recommend introducing redundancy.)
这篇关于将十六进制字符串(char [])转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文