If I have
char input[50] = "xFFFF";
int a;
How can I store the numerical value of input in a? the language is C.
One way to do it might be:
if (sscanf(input, "x%x", &a) == 0) {
/* matching failed */
}
If your input uses a real hex specifier (like "0xFFFF") you can just use %i:
if (sscanf(input, "%i", &a) == 0) {
/* matching failed */
}
You can use strtol function
char *ptr;
long a = strtol( input, &ptr, 16 );
One way:
#include <stdlib.h>
int main()
{
char *p="xFFFF";
long lng=strtol(&p[1], (char **)0, 16);
printf("%ld\n", lng);
return 0;
}
请参阅C ++将十六进制字符串转换为有符号整数 ,如果您在纯C环境中,请确保向下滚动。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.