How can I convert an integer to a hexadecimal string in C?
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"
.
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
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
Usually with printf
(or one of its cousins) using the %x
format specifier.
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;
}
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/
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
This answer is for those, who need to start from string
in decimal representation (not from int
).
- Convert your string representation of the number to an integer value (you can use
int atoi( const char * str );
function - 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;
}
精彩评论