sscanf Question

Discussion in 'C Programming' started by alij, Oct 4, 2007.

  1. alij

    alij Guest

    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
    alij, Oct 4, 2007
    #1
    1. Advertising

  2. alij <> 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.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"
    Keith Thompson, Oct 4, 2007
    #2
    1. Advertising

  3. alij

    alij Guest

    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 <> wrote:
    > alij <> 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.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, Oct 4, 2007
    #3
  4. 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
    Martin Ambuhl, Oct 4, 2007
    #4
  5. "Martin Ambuhl" <> a écrit dans le message de news:
    ...
    > 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
    Charlie Gordon, Oct 4, 2007
    #5
  6. alij

    Default User Guest

    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>
    Default User, Oct 4, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Smith

    Question on sscanf

    Dan Smith, Aug 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    406
    Martin Ambuhl
    Aug 14, 2003
  2. Brent Lievers

    sscanf fixed-width integer question

    Brent Lievers, Apr 22, 2004, in forum: C Programming
    Replies:
    8
    Views:
    629
    Dan Pop
    Apr 26, 2004
  3. sscanf() question?

    , Jun 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    503
  4. broeisi

    Programmer wannabee question about sscanf

    broeisi, Mar 4, 2006, in forum: C Programming
    Replies:
    10
    Views:
    522
    CBFalconer
    Mar 5, 2006
  5. sscanf question

    , Nov 22, 2006, in forum: C Programming
    Replies:
    5
    Views:
    623
Loading...

Share This Page