0

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?

2
  • 1
    Why are you trying to squeeze 16 characters of hex into a 12 byte string, which can only really handle 11 characters plus a null terminator. It also helps readability of output if you include newlines at appropriate points. And you would probably do better with with unsigned long long and strtoull()int won't hold 16 hex digits on most machines. Commented Apr 4, 2018 at 6:04
  • 2
    int is probably not wide enough. Your platform seems to have 32 bit ints and your input is 64 bits. Commented Apr 4, 2018 at 6:05

1 Answer 1

3

Why are you trying to squeeze 16 characters of hex into a 12 byte string, which can only really handle 11 characters plus a null terminator. It also helps readability of output if you include newlines at appropriate points. And you would probably do better with with unsigned long long and strtoull()int won't hold 16 hex digits on most machines. It is also conventional to write expected output on standard output; use standard error for error messages.

Putting those changes together yields an MCVE (Minimal, Complete, Verifiable Example) like this:

#include <stdio.h> #include <stdlib.h> int main(void) { const char lm_bin[] = "0110110000001110110000010100111000110011110110110111000110111000"; unsigned long long lm_dec = strtoull(lm_bin, NULL, 2); char lm_hex[17]; sprintf(lm_hex, "%llx", lm_dec); printf("binary %s\n", lm_bin); printf("hex %s\n", lm_hex); return 0; } 

And the output from that is what you sought, I believe:

binary 0110110000001110110000010100111000110011110110110111000110111000 hex 6c0ec14e33db71b8 

That code doesn't check for conversion errors and could use snprintf() instead of sprintf(), and could do all the printing in a single call to printf() — and doesn't need to use the string for the hex-formatted output (it could format lm_dec directly). So, there's lots of room for changes and improvements in the code.

Sign up to request clarification or add additional context in comments.

1 Comment

ahhh it is so much more to learn. I missed not only long long int but also long long hex. Naturally Your code works like charm. Thank You very much !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.