Reputation: 17390
I am trying to print out an unsigned int
as a hex, but not simply using %08X
. I need to print the hex of the MSB and the LSB for each byte with the MSB first. I have gotten that:
unsigned int num = 1234567890;
char *p = (char*)#
for (int i = 0; i < 4; i++) {
printf("%X%X", (*(p+i) & 0xF0) >> 4, *(p+i) & 0x0F);
}
// prints: D2029649
However I am having trouble going back to an unsigned int
from a string containing D2029649
:
short dehex(char hexLetter) {
if(hexLetter > 47 && hexLetter < 58) hexLetter -= 48;
if(hexLetter > 64) hexLetter -= 55;
return hexLetter;
}
unsigned int o = 0;
char *in = "D2029649";
int len = strlen(in);
for(int i = 0; i < len; i++) {
if(i % 2 == 0) {
o |= ((unsigned char)dehex(in[i]) << 4) + dehex(in[i+1]);
o <<= 8;
}
}
Needless to say, this doesn't work at all. The two lines of code manipulating o
are the result of my attempts to undo what the original encoding does and some research I did on how to turn bytes back into an int.
Perhaps my trouble comes with a complete misunderstanding of what I am actually doing when I am generating the hex.
Does anyone have any tips to get me going in the right direction to take the hex back to the original unsigned int?
Upvotes: 0
Views: 438
Reputation: 881403
The purpose of your exercise appears to be converting the integer to a string, based on its memory layout, and then converting it back.
Otherwise, you wouldn't fiddle around with trying to figure out whether you're on a byte boundary or not and then doing bit-shift "gymnastics". Rather, you'd just just left shift the value by four and add the next digit in, something like:
#include <stdio.h>
#include <string.h>
short dehex (char hexLetter) {
if ((hexLetter >= '0') && (hexLetter <= '9'))
return hexLetter - '0';
return hexLetter - 'A' + 10;
}
int main (void) {
unsigned int o = 0;
char *in = "D2029649";
int len = strlen (in);
for (int i = 0; i < len; i++)
o = (o << 4) + dehex (in[i]);
printf ("%X\n", o);
return 0;
}
You'll notice I've changed the dehex
function slightly since I'm not a big fan of magic numbers where character constants are much more readable (it's still not portable to all implementations, such as EBCDIC, but there's precious few of those around nowadays).
This program will output the unsigned integer value from that string in uppercase hex for checking: D2029649
.
However, the value 1234567890
decimal is, in hex, 499602D2
, with the order of the bytes reversed from what you have in your string. That's because you're on what's known as a little-endian architecture.
So you have two problems.
The first is that you're processing the bytes in the wrong order for getting back your original number.
You need to start at the end of the string and work backwards, something like:
for (int i = len - 2; i >= 0; i -= 2) ...
The second is that you're doing the add/shift operations in the wrong order.
Because your original code shifted after the add, it was getting to D2029649
and then shifting that left by eight bits resulting in 02964900
- the D2
shifts off the left into oblivion and a zero byte is shifted in at the right. You need to shift then add:
o <<= 8;
o |= ((unsigned char)dehex(in[i]) << 4) + dehex(in[i+1]);
Finally, though not necessary, you can simplify your code quite a bit if you just use unsigned char for returning the converted nybble. The code I would write would be along the following lines:
#include <stdio.h>
#include <string.h>
unsigned char dehex (char hexLetter) {
if ((hexLetter >= '0') && (hexLetter <= '9'))
return hexLetter - '0';
return hexLetter - 'A' + 10;
}
int main (void) {
unsigned int num = 1234567890;
char *p = (char*) #
for (int i = 0; i < 4; i++)
printf("%X%X", (*(p+i) & 0xF0) >> 4, *(p+i) & 0x0F);
putchar ('\n');
unsigned int o = 0;
char *in = "D2029649";
int len = strlen (in);
for (int i = len - 2; i >= 0; i -= 2) {
o <<= 8;
o |= (dehex (in[i]) << 4) + dehex (in[i+1]);
}
printf ("%X, or %u\n", o, o);
return 0;
}
Note that that makes a lot more sense in terms of doing it a byte at a time rather than a nybble at a time. With nybble-based code, you would have to do array indexes in the order 6, 7, 4, 5, 2, 3, 0, 1
rather than a nice little 0..7
loop.
The output of this program is:
D2029649
499602D2, or 1234567890
showing that you're getting back your original number.
Upvotes: 1