string to hex

R

RubenDV

I want to convert a message digest string like
f5d73823596b869ef5ddf38af0920688a52257b2 into an array of unsigned
chars , for example (with the previously mentioned message digest
string) :

buf[0] = 0xf5
buf[1] = 0xd7
buf[2] = 0x38
....

This way, when the message digest string is of length x, buf contains
x/2 values because each element consists of 2 hexadecimal digits.

Could somebody give me a function that does this? I have been trying
with sscanf( ) but i wasn't able to do it.

Thanks in advance!
 
E

Emmanuel Delahaye

RubenDV wrote on 20/09/05 :
I want to convert a message digest string like
f5d73823596b869ef5ddf38af0920688a52257b2 into an array of unsigned
chars , for example (with the previously mentioned message digest
string) :

buf[0] = 0xf5
buf[1] = 0xd7
buf[2] = 0x38
...

This way, when the message digest string is of length x, buf contains
x/2 values because each element consists of 2 hexadecimal digits.

Could somebody give me a function that does this? I have been trying
with sscanf( ) but i wasn't able to do it.

Saiz who ? You haven't posted a single line of code ! sscanf() can do
the job with "%2X". Don't forger to check the returned value of
sscanf().


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
W

Walter Roberson

I want to convert a message digest string like
f5d73823596b869ef5ddf38af0920688a52257b2 into an array of unsigned
chars , for example (with the previously mentioned message digest
string) :
buf[0] = 0xf5
This way, when the message digest string is of length x, buf contains
x/2 values because each element consists of 2 hexadecimal digits.
Could somebody give me a function that does this? I have been trying
with sscanf( ) but i wasn't able to do it.

You could loop using sscanf() with a "%2x" format. But watch out
for whitespace skipping.

You could also use strtoul() with a base of 16 -- but you would need
to watch out for whitespace skipping, and for the string being longer
than the receiving unsigned long, and for having to break the unsigned
long into bytes again. Probably easier not to go this route.

Probably the simplest approach is the best: read a pair of characters,
make sure they are in range, convert them from hex to unsigned int,
multiply the first member by 16 and add the second and store that result.

My implementations of this tend to be a very small state machine --
but then my implementations tend to be for situations in which there
might be newlines embedded, and my implementations usually have to deal
with the possibility that an odd number of characters was input.

A note along those lines: if you do happen to get an odd number of
characters, you will need to decide whether that should imply a leading 0
nibble or a trailing 0 nibble or that the trailing hex digit should
be treated as if it were the low nibble. Consider hex strings of length
1 and of length 3... and if embedded newlines are allowed, then
consider what should happen if the newline appears at an odd nibble
position...
 
R

RubenDV

i made sure the it prints a 0 when the hexadecimal value is smaller
than 0x10, so the number of digits is always even, so the %2x thing
should to the trick. thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,749
Members
47,302
Latest member
MitziWragg

Latest Threads

Top