Printing hexadecimal format

Discussion in 'C Programming' started by Ben Bullock, Jul 14, 2005.

  1. Ben Bullock

    Ben Bullock Guest

    Hi,

    I want to print a string of UTF8 encoded characters as two-character
    hexadecimals.

    For example I want to print Unicode 3003 as "E3 80 85".

    My problem is, what format string should I use? I tried

    printf ("%X ", c);

    but it gives me "FFFFFFFE3" not "E3". So I tried putting some numbers after
    the % but it didn't help. For example I tried

    printf ("%2X ", c);

    and

    printf ("%2.2X ", c);

    but this gave again the longer version of the string.

    It would be easy to write a small routine to output two characters of hex
    for each byte, but is there a way to do this using formats?

    Thanks for your help.

    Ben.
    Ben Bullock, Jul 14, 2005
    #1
    1. Advertising

  2. In article <db47sq$1h9$-u.ac.jp>,
    Ben Bullock <> wrote:
    >I want to print a string of UTF8 encoded characters as two-character
    >hexadecimals.


    >For example I want to print Unicode 3003 as "E3 80 85".


    >My problem is, what format string should I use? I tried


    > printf ("%X ", c);


    >but it gives me "FFFFFFFE3" not "E3".


    That tells us that c is signed instead of unsigned. You did not,
    however, happen to mention what the type of c is for your snippet.

    Generally speaking, remember that it is implementation dependant
    as to whether the char type is signed or unsigned, and if you
    need one or the other then you should be explicit in your
    declarations.

    Alternately, cast the value going into printf. For example,

    printf( "%02X", (unsigned char) c );
    --
    Would you buy a used bit from this man??
    Walter Roberson, Jul 14, 2005
    #2
    1. Advertising

  3. Ben Bullock

    Ben Bullock Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:db4a2n$qrm$...
    > In article <db47sq$1h9$-u.ac.jp>,
    > Ben Bullock <> wrote:
    >>I want to print a string of UTF8 encoded characters as two-character
    >>hexadecimals.

    >
    >>For example I want to print Unicode 3003 as "E3 80 85".

    >
    >>My problem is, what format string should I use? I tried

    >
    >> printf ("%X ", c);

    >
    >>but it gives me "FFFFFFFE3" not "E3".

    >
    > That tells us that c is signed instead of unsigned. You did not,
    > however, happen to mention what the type of c is for your snippet.


    Sorry, it's actually "char * utf" and "c" is actually "utf" where "i" is
    an int.

    I'm trying to print search strings like this:

    http://images.google.com/images?q=金文体&hl=en

    This is for a (C generated) web page:

    http://www.sljfaq.org/afaq/calligraphy.html

    (NB At the moment the links from that page to Google Images don't work.)

    > Generally speaking, remember that it is implementation dependant
    > as to whether the char type is signed or unsigned, and if you
    > need one or the other then you should be explicit in your
    > declarations.
    >
    > Alternately, cast the value going into printf. For example,
    >
    > printf( "%02X", (unsigned char) c );


    Thanks, that looks like the right answer, I'll try it.

    Have a nice Thursday,

    Ben.
    Ben Bullock, Jul 14, 2005
    #3
  4. Ben Bullock

    Sorav Bansal Guest

    > My problem is, what format string should I use? I tried
    >
    > printf ("%X ", c);
    >

    printf("%hhx",c) should do what you are looking for. 'hh' is a length
    modifier that corresponds to the length of a signed/unsigned character.
    See "man printf" for more details.

    hth
    -sorav
    Sorav Bansal, Jul 14, 2005
    #4
  5. Ben Bullock

    Ben Bullock Guest

    "Sorav Bansal" <> wrote in message
    news:db4b8m$5ch$...
    >> My problem is, what format string should I use? I tried
    >>
    >> printf ("%X ", c);
    >>

    > printf("%hhx",c) should do what you are looking for. 'hh' is a length
    > modifier that corresponds to the length of a signed/unsigned character.
    > See "man printf" for more details.


    Thanks very much, I'll try that.
    Ben Bullock, Jul 14, 2005
    #5
  6. In article <db4b8m$5ch$>,
    Sorav Bansal <> wrote:
    >> My problem is, what format string should I use? I tried


    >> printf ("%X ", c);


    >printf("%hhx",c) should do what you are looking for. 'hh' is a length
    >modifier that corresponds to the length of a signed/unsigned character.
    >See "man printf" for more details.


    Is hh perhaps a C99-ism? I do not see it on my C89 system.
    I do see h (single-h) there: %hx would correspond to unsigned short.
    --
    Any sufficiently advanced bug is indistinguishable from a feature.
    -- Rich Kulawiec
    Walter Roberson, Jul 14, 2005
    #6
  7. Walter Roberson wrote:
    > In article <db4b8m$5ch$>,
    > Sorav Bansal <> wrote:
    > >> My problem is, what format string should I use? I tried

    >
    > >> printf ("%X ", c);

    >
    > >printf("%hhx",c) should do what you are looking for. 'hh' is a length
    > >modifier that corresponds to the length of a signed/unsigned character.
    > >See "man printf" for more details.

    >
    > Is hh perhaps a C99-ism? I do not see it on my C89 system.
    > I do see h (single-h) there: %hx would correspond to unsigned short.


    Yes, the hh modifier was introduced in C99. %hhx would correspond to
    unsigned char.

    Robert Gamble
    Robert Gamble, Jul 14, 2005
    #7
  8. On Thu, 14 Jul 2005 01:16:17 +0000 (UTC), Walter Roberson
    <-cnrc.gc.ca> wrote:

    > In article <db4b8m$5ch$>,
    > Sorav Bansal <> wrote:
    >>> My problem is, what format string should I use? I tried

    >
    >>> printf ("%X ", c);

    >
    >>printf("%hhx",c) should do what you are looking for. 'hh' is a length
    >>modifier that corresponds to the length of a signed/unsigned character.
    >>See "man printf" for more details.

    >
    > Is hh perhaps a C99-ism? I do not see it on my C89 system.
    > I do see h (single-h) there: %hx would correspond to unsigned short.


    It is, and so can't be depended on unless you know that the library (not
    just the compiler) supports it. Since many compilers (especially on
    Unix systems) use the system's library the version of the compiler and
    that of the library often don't match (and there is no way to determine
    automatically which functionality may be missing without doing very
    extensive -- and expensive -- testing).

    Chris C
    Chris Croughton, Jul 14, 2005
    #8
    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. Tom McCarthy

    Hexadecimal date format

    Tom McCarthy, Nov 15, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,811
    Tom McCarthy
    Nov 15, 2003
  2. mathieu
    Replies:
    1
    Views:
    2,689
    mathieu
    Jan 6, 2007
  3. Maxime Barbeau

    printing uin32_t in hexadecimal

    Maxime Barbeau, Aug 13, 2004, in forum: C Programming
    Replies:
    10
    Views:
    1,397
    Maxime Barbeau
    Aug 17, 2004
  4. Tobias Rischer

    Problem printing Hexadecimal

    Tobias Rischer, Mar 19, 2007, in forum: C Programming
    Replies:
    14
    Views:
    595
    Peter Shaggy Haywood
    Mar 24, 2007
  5. Diwa
    Replies:
    6
    Views:
    2,325
    Francesco S. Carta
    Oct 2, 2009
Loading...

Share This Page