Thanks for the advice. It works. But I met a strange thing.

suppose nsStr has been init. And myConvert is a function using your code.

FILE *fd = fopen(..., "a");
fputws(myConvert(nsStr), fd);
fprintf(fd, "aa\n");
fclose(fd);

When I ran the above code, the string is outputed into file, but "aa\n" is
not.

Then I tried following:
FILE *fd = fopen(..., "a");
fputws(myConvert(nsStr), fd);
fclose(fd);
fopen(..., "a"); //open again
fprintf(fd, "aa\n");
fclose(fd);

This time, "aa\n" can be outputed too.

So does "fputws" cause problem here? If I want to output wide character
string and also char string, is there better way then my "solution"?

thanks.


"Benjamin D. Smedberg" <[EMAIL PROTECTED]> wrote
news:[EMAIL PROTECTED]
> banli wrote:
> > say if I have a variable nsStr of type of nsEmbedString, could anyone
tell
> > me how to convert the return value of nsStr.get() to char*? And how to
> > output the return value of nsStr.get() to file?
> >
> > I tried to output the return value to file using fputws(nsStr.get(),
fd).
> > But the compiler complains with "cannot convert" 'const PRUnichar*' to
> > 'const wchar_t*'.
>
> Unfortunately, there is not a way to to UTF16->UTF8 conversions without
> linking to non-frozen stuff (yet). Darin and I are trying to figure out
> the minimum necessary linkage to accomplish these kinds of tasks.
>
> You can't cast PRUnichar* to wchar_t* because on most *ixes, wchar_t is
> a four-byte type. However, you can safely do a funky copy-conversion to
> a wchar_t type:
>
> PRUnichar *start = nsStr.get();
> PRUnichar *end nsStr.get() + nsStr.Length();
> wchar_t *wstart = new wchar_t[nsStr.Length()];
> wchar_t *wend = start + nsStr.Length();
>
> for(; start < end; ++start) {
>    *wstart = (wchar_t) *start;
>    ++wstart;
> }
> *wstart = 0;
>
> There are also some interfaces in intl (charset conversion) that might
> do the trick for you.
>
> --BDS


_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to