sscanf Question

A

alij

Hi,
Given the following statement:

sscanf(src, "hh02x", &dest);

The part I don't understrand is, whats the 'hh' for? I know %h is for
hexadecimal extraction (why two h?). Also, whats the use of '02'?

Looking forward to any help/pointers,

Thanks,
Alij
 
K

Keith Thompson

alij said:
Given the following statement:

sscanf(src, "hh02x", &dest);

The part I don't understrand is, whats the 'hh' for? I know %h is for
hexadecimal extraction (why two h?). Also, whats the use of '02'?

No, I'm afraid you don't know what the 'h' is for. It doesn't mean
hexadecimal; that's what the 'x' means.

Since there's no '%' character in the format string, it doesn't
actually read anything. This illustrates why you should
copy-and-paste your exact code. By re-typing it, you've managed to
lose the most important part of it.

Probably the actual statement is:

sscanf(src, "%hh02x", &dest);

and presumably dest is declared to be of type unsigned char.

You should read the documentation for the sscanf function. If your
system doesn't provide it (try "man sscanf" if you're on a Unix-like
system), try Google.

"%x" specifies hexaecimal input. The "02" in front of it specifies
the maximum field width (I'm not sure whether the leading "0" is
meaningful). The "hh" says the corresponding argument is a pointer to
unsigned char rather than a pointer to unsigned int. (With a single
"h", it would be a pointer to unsigned short.)

The 'h' was probably chosen because it's the first letter of the word
"half", but that's just a mnemonic, not the actual meaning, since a
short isn't necessarily have the size of an int, and a char isn't
necessarily have the size of a short.

Read The Fine Manual.
 
A

alij

Hi,

Thank you for the explanation! You are right, I should have just
pasted the code -- sorry about that.

Much appreciated,
Alij
 
M

Martin Ambuhl

alij said:
Hi,
Given the following statement:

sscanf(src, "hh02x", &dest);

The part I don't understrand is, whats the 'hh' for? I know %h is for
hexadecimal extraction (why two h?). Also, whats the use of '02'?

You know something that is completely false. %x is for hex, %h and %hh
have nothing to do with that: they are _length_ modifiers.
%x is for hex representation of an unsigned int,
%hx is for hex representation of an unsigned short,
%hhx is for hex representation of an unsigned char.
Also look up the forms %lx, %llx, %jx, %zx, and %tz
 
C

Charlie Gordon

Martin Ambuhl said:
You know something that is completely false. %x is for hex, %h and %hh
have nothing to do with that: they are _length_ modifiers.
%x is for hex representation of an unsigned int,
%hx is for hex representation of an unsigned short,
%hhx is for hex representation of an unsigned char.
Also look up the forms %lx, %llx, %jx, %zx, and %tz

The last one should read %tx of course.
 

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

Similar Threads

sscanf Question 6
sscanf question 14
problem with sscanf in email client 4
Help with sscanf() needed desperately 8
the usage of sscanf 5
First time question 1
Question on sscanf 3
Hello to all! 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top