Valhalla Legends Forums Archive | C/C++ Programming | Char -> Hex Conv.

AuthorMessageTime
gameschild
Using the basic Linux library calls (unistd.h) i need to convert 8 Char's to a 16digit hex. At the mo ive copied them into a seperate Char buffer[8] section but still stuck with the Hex conversion.

Arta had the same problem and fixed but my phone is crap and cant talk long enough to figure it out.

Any help would be great.
December 28, 2003, 2:35 AM
Kp
This should work. Let me know if you want more explanations. Also note that you might need to make char unsigned to avoid weird behaviors for characters above 127 in value.

[code]
// input: a number in the range [0,15]
// output: hex representation of that number
// (['0', '9'] for input in [0,9], ['a', 'f'] for input in [10, 15])
static inline char tohex (char c) {
return c + '0' + (c > 9 ? 'a' - '0' : 0); }

/* then in your function */
unsigned j = 0;
for (; j < 8; ++j) {
outbuf[2*j] = tohex (inbuf[j] >> 4); // get the upper nibble
outbuf[(2*j)+1] = tohex (inbuf[j] & 15); // get the lower nibble
}[/code]

[Edit: forum attempted to wrap one of my comments, making it appear to be code.]
December 28, 2003, 2:51 AM
gameschild
the code works great, thanks. one question though, could u explain the tohex to i can move it into the for loop. this way i can keep it all in one function when i optimise it as the code is fairy small anyway.

thanks man.
December 28, 2003, 3:06 AM
gameschild
just ran on a different file and getting G+ letters in there. any idea why?
December 28, 2003, 3:11 AM
gameschild
outbuf[(2*j)+1] = tohex (inbuf[j] & 15); //

is this line right? just a guess (and it really is) that the "& 15" is off?
December 28, 2003, 3:16 AM
Kp
First, learn to use edit to update your messages instead of replying many times. This is not a chat room.

tohex(char) does as its comment states. It converts a number into the hex char which represents that number. You're better off leaving it as a separate function, IMO. It can be useful in other places. Besides which, if you leave it marked as static inline, gcc will integrate it into the loop for you - it'll be as though you expanded it, but you won't have to deal with two copies.

Finally, the bad letters are likely because you didn't heed my warning about signedness. If the character in inbuf has a value greater than 127 and you are using signed characters, your result will probably be wrong. Switch to unsigned characters.
December 28, 2003, 4:00 AM
gameschild
it was 4.30am - rational thought it usually out the window by i apologise and the unsigned bit works now. thanks.

(as the title says - newbie)
December 28, 2003, 2:59 PM
Arta
hmm, why static? Seems pointless/paranoid. Not that that would be unusual, or anything :P

Addition:

How is that function supposed to work, kp? Perhaps I'm missing something but it seems broken to me. Am rather tired though. Changed to this, which works ok:

[code]
inline char tohex(unsigned char c)
{   
return (c > 9 ? c + 87 : c + '0');
}
[/code]
December 28, 2003, 3:03 PM
Kp
[quote author=Arta[vL] link=board=30;threadid=4466;start=0#msg37332 date=1072623827]
hmm, why static? Seems pointless/paranoid. Not that that would be unusual, or anything :P[/quote]

gcc will not emit assembler code for a static inline function if all references to the function were successfully inlined. If the function is inline but nonstatic, there is the possibility of external references to the function, which would require that the function exist as assembler code for those external references to link right.

[quote author=Arta[vL] link=board=30;threadid=4466;start=0#msg37332 date=1072623827]
How is that function supposed to work, kp? Perhaps I'm missing something but it seems broken to me. Am rather tired though. Changed to this, which works ok:

[code]inline char tohex(unsigned char c)
{
return (c > 9 ? c + 87 : c + '0');
}[/code][/quote]

The function is supposed to work correctly, of course. :) It takes c and adds '0' to it, thus converting a number in the range [0, 15] to ['0', '?'] (see an ASCII chart for why). Then, if the pre-add number was greater than 9, we add ('a' - '0'), which is the same as undoing the previous add (bringing the [10,15] number back into that range), then adding 'a', which converts it into the range ['a', 'f'].

Also, mine has the advantage of using character constants for all its math, so it's easier to see what's going on. :)
December 28, 2003, 6:10 PM
Skywing
Doesn't the linker remove unreferenced things?
December 28, 2003, 6:30 PM
Kp
[quote author=Skywing link=board=30;threadid=4466;start=0#msg37349 date=1072636238]Doesn't the linker remove unreferenced things?[/quote]

Probably depends on the linker, and on how the unreferenced thing was brought in on the command line (whether it's a member of an archive, in a standalone file with no other referenced symbols, in a needed file, etc.). I've never specifically checked whether gcc's default linker will do so or not.

[Edit: ld associated with the latest mingw gcc [3.3.1] does not remove unreferenced items which appear in files which have referenced symbols. However, if the symbol is static, gcc will warn you if you don't refer to it.]
December 28, 2003, 7:02 PM
Arta
well, didn't work for us. Instead of getting a-f, we got k-whatever :P
December 28, 2003, 11:52 PM
Kp
Minor bugfix required. Change tohex as follows:
[code]return c + '0' + (c > 9 ? 'a' - '0' - 10 : 0);[/code]
December 29, 2003, 12:08 AM
Adron
[quote author=Skywing link=board=30;threadid=4466;start=0#msg37349 date=1072636238]
Doesn't the linker remove unreferenced things?
[/quote]

I think you need to enable function-level linking to remove individual functions?


January 3, 2004, 2:27 PM

Search