RE: How to print the byte representation of a wchar_t string with non -ASCII ...

From: Jungshik Shin (jshin@mailaps.org)
Date: Fri Nov 02 2001 - 20:07:25 EST


 Hi,

  Since Addison already wrote about one aspect of your problem,
I'll raise another issue.

> mbstowcs(wstr, argv[1], 20);
> printf("stdin in WC: %ls, wcslen: %d\n", wstr, wcslen(wstr));
> // Guess this is the only way to see the byte rep of wstr string

   This is not the case. You may have missed one of replies to
your previous question. What you're doing in your code is not printing
out byte rep. of wstr but just printing out byte rep. of mstr (this is
not so useful unless you want to check that mbstwcs() and wcstombs()
correctly round-trip the original argv[1]).

> wcstombs(mstr, wstr, 20);
> printf("Byte rep: ");
> for (i = 0; i < strlen(mstr); i++)
> printf("%02X ", mstr[i]);

  To print out the byte rep. of wstr, you have to do the following as
suggested in the message you may have missed.

        for (i=0; wstr[i] != L'\0'; ++i)
                printf (" %0*X", 2*sizeof (wchar_t), wstr[i]);
        putchar('\n');

  Jungshik Shin



This archive was generated by hypermail 2.1.2 : Fri Nov 02 2001 - 21:00:48 EST