Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在AVR Studio中将十六进制转换为十进制?_C_Hex_Decimal_Avr - Fatal编程技术网

C 在AVR Studio中将十六进制转换为十进制?

C 在AVR Studio中将十六进制转换为十进制?,c,hex,decimal,avr,C,Hex,Decimal,Avr,如何在AVR Studio中将十六进制无符号字符类型转换为十进制整数类型 有任何内置函数可用于这些吗?在AVRs上,我在使用传统的十六进制2 int方法时遇到问题: char *z="82000001"; uint32_t x=0; sscanf(z, "%8X", &x); 或 他们只是提供了错误的输出,没有时间调查原因 因此,对于AVR微控制器,我编写了以下函数,包括相关注释,以便于理解: /** * hex2int * take a hex string and conver

如何在AVR Studio中将十六进制无符号字符类型转换为十进制整数类型


有任何内置函数可用于这些吗?

在AVRs上,我在使用传统的十六进制2 int方法时遇到问题:

char *z="82000001";
uint32_t x=0;
sscanf(z, "%8X", &x);

他们只是提供了错误的输出,没有时间调查原因

因此,对于AVR微控制器,我编写了以下函数,包括相关注释,以便于理解:

/**
 * hex2int
 * take a hex string and convert it to a 32bit number (max 8 hex digits)
 */
uint32_t hex2int(char *hex) {
    uint32_t val = 0;
    while (*hex) {
        // get current character then increment
        char byte = *hex++; 
        // transform hex character to the 4bit equivalent number, using the ascii table indexes
        if (byte >= '0' && byte <= '9') byte = byte - '0';
        else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
        else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;    
        // shift 4 to make space for new digit, and add the 4 bits of the new digit 
        val = (val << 4) | (byte & 0xF);
    }
    return val;
}
将输出:

编辑:sscanf也适用于AVR,但对于大十六进制数,您需要使用%lX,如下所示:

char *z="82000001";
uint32_t x=0;
sscanf(z, "%lX", &x);

当涉及整数类型时,十六进制和十进制只是人类的不同表示形式,实际数据总是二进制的。因此,在编译源代码时,无论您在源代码中编写0x0A还是10,都没有区别??;int i=b;你的问题不清楚。十六进制无符号字符类型到底是什么意思?单个十六进制数字的ASCII表示形式?字符串,即ASCII字符数组?或者只是一个二进制数,就像这里其他人建议的那样?举个例子也许会有帮助,但当我执行算术运算时,我并没有得到预期的结果。那么,如何将使用串行通信接收的十六进制字符串转换为相应的整数呢?请尝试sscanfhex_字符串、%x、&my_unsigned_int;,或者my_int=strotalhex_字符串,NULL,16;。
char *z ="82ABC1EF";
uint32_t x = hex2int(z);
printf("Number is [%X]\n", x);
char *z="82000001";
uint32_t x=0;
sscanf(z, "%lX", &x);