Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sscanf Question (http://www.velocityreviews.com/forums/t541833-sscanf-question.html)

alij 10-03-2007 11:52 PM

sscanf Question
 
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


Keith Thompson 10-04-2007 01:21 AM

Re: sscanf Question
 
alij <aliasger.jaffer@gmail.com> 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) kst-u@mib.org <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"

alij 10-04-2007 02:39 AM

Re: sscanf Question
 
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 <ks...@mib.org> wrote:
> alij <aliasger.jaf...@gmail.com> 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) ks...@mib.org <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"




Martin Ambuhl 10-04-2007 04:51 AM

Re: sscanf Question
 
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

Charlie Gordon 10-04-2007 10:09 AM

Re: sscanf Question
 
"Martin Ambuhl" <mambuhl@earthlink.net> a écrit dans le message de news:
5mj9qiFd5sufU1@mid.individual.net...
> 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



Default User 10-04-2007 08:22 PM

Re: sscanf Question - TPA
 
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>


All times are GMT. The time now is 12:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.