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

时间:2021-12-10 20:28:38

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

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

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

示例:整数50将转换为十六进制字符串“32”或“0x32”。

6 个解决方案

#1


36  

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

#2


17  

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

通常使用%x格式说明符的printf(或其中一个表兄弟)。

#3


15  

The following code takes an integer and makes a string out of it in hex format:

以下代码采用整数形式,并以十六进制格式生成一个字符串:

int  num = 32424;
char hex[5];

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

gives

7ea8

#4


7  

Interesting that these answers utilize printf like it is a given. printf converts 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;

}

#5


3  

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

我做了一个librairy来进行十六进制/十进制转换而不使用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/

#6


1  

To convert an integer to a string also involves char array 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 string is 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

#1


36  

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

#2


17  

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

通常使用%x格式说明符的printf(或其中一个表兄弟)。

#3


15  

The following code takes an integer and makes a string out of it in hex format:

以下代码采用整数形式,并以十六进制格式生成一个字符串:

int  num = 32424;
char hex[5];

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

gives

7ea8

#4


7  

Interesting that these answers utilize printf like it is a given. printf converts 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;

}

#5


3  

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

我做了一个librairy来进行十六进制/十进制转换而不使用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/

#6


1  

To convert an integer to a string also involves char array 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 string is 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