Printing hexadecimal format

B

Ben Bullock

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.
 
W

Walter Roberson

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 );
 
B

Ben Bullock

Walter Roberson said:
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.
 
S

Sorav Bansal

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
 
B

Ben Bullock

Sorav Bansal said:
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.
 
W

Walter Roberson

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.
 
R

Robert Gamble

Walter said:
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
 
C

Chris Croughton

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
 

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

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top