C语言 如何在C中将整数转换为十六进制字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3464194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:09:30  来源:igfitidea点击:

How can I convert an integer to a hexadecimal string in C?

c

提问by user417894

How can I convert an integer to a hexadecimal string in C?

如何在C中将整数转换为十六进制字符串?

Example: The integer 50would be converted to the hexadecimal string "32"or "0x32".

示例:整数50将被转换为十六进制字符串"32""0x32".

回答by C Johnson

This code

这段代码

int a = 5;
printf("%x\n", a);

prints

印刷

5


This code

这段代码

int a = 5; 
printf("0x%x\n", a);

prints

印刷

0x5


This code

这段代码

int a = 89778116;
printf("%x\n", a);

prints

印刷

559e7c4


If you capitalize the x in the format it capitalizes the hex value:

如果在格式中将 x 大写,则它会将十六进制值大写:

int a = 89778116;
printf("%X\n", a);

prints

印刷

559E7C4


If you want to print pointers you use the p format specifier:

如果要打印指针,请使用 p 格式说明符:

char* str = "foo";
printf("0x%p\n", str);

prints

印刷

0x01275744

回答by Pithikos

The following code takes an integerand makes a stringout of it in hex format:

以下代码接受一个整数并以十六进制格式将其生成一个字符串

int  num = 32424;
char hex[5];

sprintf(hex, "%x", num);
puts(hex);

gives

7ea8

回答by Jerry Coffin

Usually with printf(or one of its cousins) using the %xformat specifier.

通常与printf(或其表亲之一)使用%x格式说明符。

回答by dlb

Interesting that these answers utilize printflike it is a given. printfconverts the integer to a Hexadecimal string value.

有趣的是,这些答案printf就像给定的一样使用。 printf将整数转换为十六进制字符串值。

//*************************************************************
// void prntnum(unsigned long n, int base, char sign, char *outbuf)
// unsigned long num = number to be printed
// int base        = number base for conversion;  decimal=10,hex=16
// char sign       = signed or unsigned output
// char *outbuf   = buffer to hold the output number
//*************************************************************

void prntnum(unsigned long n, int base, char sign, char *outbuf)
{

    int i = 12;
    int j = 0;

    do{
        outbuf[i] = "0123456789ABCDEF"[num % base];
        i--;
        n = num/base;
    }while( num > 0);

    if(sign != ' '){
        outbuf[0] = sign;
        ++j;
    }

    while( ++i < 13){
       outbuf[j++] = outbuf[i];
    }

    outbuf[j] = 0;

}

回答by kevmuret

I made a librairy to make Hexadecimal / Decimal conversion without the use of stdio.h. Very simple to use :

我制作了一个库来进行十六进制/十进制转换而不使用stdio.h. 使用非常简单:

char* dechex (int dec);

This will use calloc()to to return a pointer to an hexadecimal string, this way the quantity of memory used is optimized, so don't forget to use free()

这将用于calloc()返回一个指向十六进制字符串的指针,这样优化了使用的内存量,所以不要忘记使用free()

Here the link on github : https://github.com/kevmuret/libhex/

这里是 github 上的链接:https: //github.com/kevmuret/libhex/

回答by chux - Reinstate Monica

To convert an integer to a stringalso involves chararray or memory management.

将整数转换为字符串还涉及char数组或内存管理。

To handle that part for such short arrays, code could use a compound literal, since C99, to create array space, on the fly. The stringis valid until the end of the block.

为了处理这种短数组的那部分,代码可以使用复合文字,从 C99 开始,即时创建数组空间。该字符串在块结束之前一直有效。

#define UNS_HEX_STR_SIZE ((sizeof (unsigned)*CHAR_BIT + 3)/4 + 1)
//                         compound literal v--------------------------v
#define U2HS(x) unsigned_to_hex_string((x), (char[UNS_HEX_STR_SIZE]) {0}, UNS_HEX_STR_SIZE)

char *unsigned_to_hex_string(unsigned x, char *dest, size_t size) {
  snprintf(dest, size, "%X", x);
  return dest;
}

int main(void) {
  // 3 array are formed v               v        v
  printf("%s %s %s\n", U2HS(UINT_MAX), U2HS(0), U2HS(0x12345678));
  char *hs = U2HS(rand());
  puts(hs);
  // `hs` is valid until the end of the block
}

Output

输出

FFFFFFFF 0 12345678
5851F42D