48

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

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

AppleDash
  • 1,544
  • 2
  • 13
  • 27
user417894
  • 491
  • 1
  • 4
  • 4

7 Answers7

53

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:

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

prints

559E7C4

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

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

prints

0x01275744
C.J.
  • 15,637
  • 9
  • 61
  • 77
  • 3
    I would recommend using the `#` modifier instead of writing your own `0x`, especially with `%p` whose behavior is implementation-defined and might already include the `0x` for you. – R.. GitHub STOP HELPING ICE Aug 12 '10 at 03:34
  • 2
    Might want to mention `snprintf`, too. – strager Aug 12 '10 at 04:25
  • 1
    Also, the format of your answer is confusing with the alternating blocks. It may be useful to have just one code block with the results as comments after the statement. – strager Aug 12 '10 at 04:26
  • @R.. Note that with `#`, the `"0x"` is not pre-pended when the value is 0. `printf("%#X\n", 0);` --> `0`. IMO, `printf("0x%X\n", a);` is worthy, keeping the `x` as lower case and `A-F` as upper. `0x559E7C4` – chux - Reinstate Monica Jun 15 '17 at 15:57
24

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
Pithikos
  • 18,827
  • 15
  • 113
  • 136
20

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

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
15

Interesting that these answers utilize printf like it is a given. printf converts the integer to a Hexadecimal string value.

//*************************************************************
// 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;

}
olibiaz
  • 2,551
  • 4
  • 29
  • 31
dlb
  • 175
  • 1
  • 2
3

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

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()

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

kevmuret
  • 51
  • 5
1

To convert an integer to a string also involves char array or memory management.

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.

#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
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

This answer is for those, who need to start from string in decimal representation (not from int).

  1. Convert your string representation of the number to an integer value (you can use int atoi( const char * str ); function
  2. Once you have your integer you can print it as HEX using, for example, sprintf function with %x as a format parameter and you integer as a value parameter

Here is a working example: https://ideone.com/qIoHNW

#include <stdio.h>
 
int main(void) { 
    int n;
    char hex_val[50];
 
    n = atoi("100663296");
    sprintf(hex_val, "%x", n);
 
    printf("%s", hex_val);
    return 0;
}
Robson
  • 813
  • 5
  • 21
  • 40