Re: How to print the byte representation of a wchar_t string with non -ASCII chars?

From: Jonathan Coxhead (jonathan@doves.demon.co.uk)
Date: Wed Oct 31 2001 - 15:07:59 EST


On 31 Oct 2001, at 10:37, Tay, William wrote:

> Hi,
>
> For debugging purpose, I'd like to find out how I can print the byte
> representation of a wchar_t string.
>
> Say in C, I have wchar_t wstr[10] = L"fran";
> Is there any printf or wchar equivalent function (using appropriate format
> template) that prints out the string as
> 66 72 C3 A1 6E in en_US.UTF-8 locale under UNIX?
>
> Like for an ASCII char, I can do printf("%x", toasii('a')); to get 61 (the
> byte rep of 'a');

   Note that toascii() is basically just a function that returns its argument,
It works in C because "char" values promote to "int". "wchar_t" values have the
same property, so you can write

      for (i = 0; wstr [i] != 0; ++i)
          printf (" %.*x", 2*sizeof wstr [0], wstr [i]);

   If wchar_t is 16 bits (UTF-16), this will print 4 hex digits for each
character; if it's 32 bits (UTF-32), it'll print 8.

   If wchar_t is one of these encodings, you'll see the encoded form, which is
probably what you want for debugging purposes.

> Would appreciate your help.
>
> Will

        /|
 o o o (_|/
        /|
       (_/



This archive was generated by hypermail 2.1.2 : Wed Oct 31 2001 - 15:59:59 EST