Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf Question

Reply
Thread Tools

sscanf Question

 
 
alij
Guest
Posts: n/a
 
      10-03-2007
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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-04-2007
alij <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
alij
Guest
Posts: n/a
 
      10-04-2007
Hi,

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

Much appreciated,
Alij

On Oct 3, 8:21 pm, Keith Thompson <(E-Mail Removed)> wrote:
> alij <(E-Mail Removed)> writes:
> > 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.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"



 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-04-2007
alij wrote:
> 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
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      10-04-2007
"Martin Ambuhl" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> alij wrote:
>> 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


The last one should read %tx of course.

--
Chqrlie


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-04-2007
alij wrote:

> Hi,


Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf question sieg1974@yahoo.com C Programming 5 11-23-2006 07:39 PM
Programmer wannabee question about sscanf broeisi C Programming 10 03-05-2006 05:16 AM
sscanf() question? jchludzinski@gmail.com C Programming 5 06-09-2005 05:45 PM
sscanf fixed-width integer question Brent Lievers C Programming 8 04-26-2004 05:55 PM
Question on sscanf Dan Smith C Programming 3 08-14-2003 12:42 AM



Advertisments