I am new to C and I have already tried many ways to convert char* variable which store binary representation into hex value.
The variable is for example:
0110110000001110110000010100111000110011110110110111000110111000 which stands for
6C 0E C1 4E 33 DB 71 B8 where
0110- 6, 1100 - C, 0000 - 0, 1110 - E and so on... However when I want to use fprintf format or strtol, which I found recommended for this kind of problems, I get:
char* lm_bin = "0110110000001110110000010100111000110011110110110111000110111000"; //convert to dec int lm_dec = (int)strtol(lm_bin, NULL, 2); char lm_hex[12]; //convert to hex sprintf(lm_hex, "%x", lm_dec); fprintf(stderr, "binary %s", lm_bin); fprintf(stderr, "hex %s", lm_hex); the output is:
binary 0110110000001110110000010100111000110011110110110111000110111000 hex 33db71b8
which is not correct. Can anyone point me where I am making a mistake?
unsigned long longandstrtoull()—intwon't hold 16 hex digits on most machines.intis probably not wide enough. Your platform seems to have 32 bitints and your input is 64 bits.