I have the following numbers which I read from a file.
These numbers are read as strings
. I need to represent them as hexadecimal
numbers.
How do I do that?
00000004
00000008
00000009
0000000d
00000100
I have the following numbers which I read from a file.
These numbers are read as strings
. I need to represent them as hexadecimal
numbers.
How do I do that?
00000004
00000008
00000009
0000000d
00000100
Since you want to read these numbers from a file, fscanf
is helpful for this:
/* note, if you're using more than 8 digits,
this would have to be unsigned long long and instead of %lx you would need %llx */
unsigned long h;
if (fscanf(f, "%lx", &h) == 1)
do whatever with h
else
error, check errno
You can use this check in a loop while (fscanf(f, "%x\n", &h) == 1);
to read until the last number.
If you have already read them into a character buffer, you could use strtol to convert from base 16 (pass 16 as the 3rd parameter).
Alternatively, if you are reading them from the file, you could fscanf with the %x
conversion specifier.