簡體   English   中英

使用sp中的sprintf進行十六進制到十進制轉換

[英]Hexadecimal to decimal conversion with sprintf in C

我有

sprintf(ascii,"%X",number) // number = 63 is a decimal integer`

char ascii[] = {51, 70, 0, 0, 0, .... }顯示為“3F”

當我有

value = atoi(ascii);

它返回value = 3而不是63。

我想要的是使用sprintf進行十六進制轉換,顯示它然后將表中的值作為十進制保存到另一個變量。

怎么做?

問題是atoi不解析十六進制。 您需要一個解析十六進制數字的函數。 想到了sscanf(ascii, "%x", &i) ......

正如其他人指出的那樣atoi沒有解析hex。 你可以試試這個:

#include <stdio.h>
int main()
{
    char s[] = "3F";
    int x;
    sscanf(s, "%x", &x);
    printf("%u\n", x);
}

IDEONE SAMPLE

或者你可以像這樣使用strol

printf("%u\n", strtol("3F", NULL, 16));

IDEONE SAMPLE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM