简体   繁体   English

如何将十六进制的字符串表示形式转换为c中的十六进制值?

[英]How do I convert a string representation of a hex to its hex value in c?

If I have 如果我有

char input[50] = "xFFFF";
int a;

How can I store the numerical value of input in a? 如何在a中存储输入的数值? the language is C. 语言是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: 如果您的输入使用真正的十六进制说明符(例如“ 0xFFFF”),则可以仅使用%i:

if (sscanf(input, "%i", &a) == 0) {
    /* matching failed */
}

You can use strtol function 您可以使用strtol函数

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环境中,请确保向下滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C编程将十六进制的字符串表示形式转换为十六进制 - C Programming convert String representation of Hex to Hex 如何将 C 二进制缓冲区转换为 Python 字符串中的十六进制表示? - How to convert a C binary buffer to it’s hex representation in Python string? 如何将C代码转换为程序集的十六进制表示形式? - How do you convert c code into assembly's hex representation? 如何在C中将二进制/十六进制数组转换为字符串数组 - How do I convert binary/hex array to string array in C 如何使用C将二进制字符串转换为十六进制? - How do I convert a binary string to hex using C? 如何在C中将字符串转换为十六进制值 - How to convert string to hex value in C 如何将u32_t中的此十六进制值转换为其对应的char / ascii值? - How do I convert this hex value in a u32_t to its corresponding char/ascii value? 如何将 hex 文件转换为其等效的 char 文件? - How do I convert a hex file to its equivalent char file? 我如何使用C语言将整数值转换为其对应的十六进制并将其存储在单字节变量中 - How can i convert an integer value into its corresponding hex and store it in a single byte variable using C language 如何在C中将包含UTF-8十六进制值的字符串转换为wchar? - How to convert a string containting the hex value of a UTF-8 to a wchar in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM