What's the "%02X" specifier doing?

T

thomas

Hi,

----------code----
int main(){

char Md5Str[34] = "670B14728AD9902AECBA32E22FA4F6BDx";
char Md5Value[1];
sscanf(Md5Str, "%02X", &Md5Value[0]);
}
--------------------
It gives me "run-time check failure #2 stack around the variable was
corrupted" error.
But I expected the Md5Value array is large enough to hold the only
value I interested in.
I searched the Internet and someone says that sscanf will append a
'\0' character in the end, so Md5Value size should be at least two.
But I still got error until Md5Value size is as large as 4. Also no
error suggested for the following code also makes me confused.

-----------------------------
int main(){

char Md5Str[34] = "670B14728AD9902AECBA32E22FA4F6BDx";
char Md5Value[1];
sscanf(Md5Str, "%c", &Md5Value[0]);
}
----------------------------
 
J

Jens Thoms Toerring

thomas said:
----------code----
int main(){
char Md5Str[34] = "670B14728AD9902AECBA32E22FA4F6BDx";
char Md5Value[1];
sscanf(Md5Str, "%02X", &Md5Value[0]);
}
--------------------
It gives me "run-time check failure #2 stack around the variable was
corrupted" error.
But I expected the Md5Value array is large enough to hold the only
value I interested in.

That may be the case but with "%X" you are telling sscan() that
it is supposed to read an unsigned int (in hex format) and thus
you must supply it with the address of an unsigned int variable
to which it can copy the value it just converted from the input.
Passing anything else then the kind of pointer it's led to expect
from the format string will lead to nasty bugs and you won't get
any warnings since there's no way sscanf() can check if the poin-
ters it received have the correct types.

The '0' in "%02X" doesn't buy you anything, "%2X" will do fine
since it does tell sscanf() to read not more than 2 characters
that could be part of an unsigned int value in hex format (of
which '0' is an acceptable character).
I searched the Internet and someone says that sscanf will append a
'\0' character in the end, so Md5Value size should be at least two.

This it will do if you tell sscanf() to read in a (C) string,
using the "%s" conversion specifier, but not for any other.
But I still got error until Md5Value size is as large as 4.

Yes, because if you use the "%X" conversion specifier sscanf()
expects to receive a pointer to an (unsigned) int and will
write to as many bytes as the size of an unsigned int is,
which seems to be 4 on your machine. There's no way around
that.
Also no error suggested for the following code also makes me confused.
char Md5Str[34] = "670B14728AD9902AECBA32E22FA4F6BDx";
char Md5Value[1];
sscanf(Md5Str, "%c", &Md5Value[0]);
}
----------------------------

Now that's something completely different. This tells sscanf()
to read in a single character. But that doesn't seem to be what
you had in mind if I understood your intentions correctly, i.e.
that you want the first two digits ('6' and '7') in the string
to be interpreted as an unsigned (hex) int and the result stored
in a char. What '%c" will result in is that you get the character
'6' into the first (and only) element of the array Md5Value. (BTW,
why are you using an array here at all? That doesn't look too
useful when it hs hust one element and thus a simple char would
do as well.)

I my guess about your intentions is correct then you need to
use an extra variable in the following way:

int main( )
{
char Md5Str[ ] = "670B14728AD9902AECBA32E22FA4F6BDx";
char Md5Value;
unsigned int tmp;
sscanf( Md5Str, "%2X", &tmp );
Md5Value = tmp;
}

Afterwards 'Md5Value' should contain the character with
the value 0x67 (i.e. a 'g' if your machine is using ASCII).

Regards, Jens
 

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
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top