A simple question - how to convert from UTF8 to wide char (wchar_t) on linux

  • Follow


Hi,

I need to convert a string from UTF8 to wide character (wchar_t *). I
perform the same in windows using:

     MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen);

However, in linux this API is not available. However, there exists
mbstowcs() API, which converts multibyte string to wide character. But
will this API convert UTF8 encoded string to wide character? Or this
API will convert *only ASCII* characters to wide characters?

There exists also iconv() API which converts characterset using a
characterset conversion descriptor returned by iconv_open(). But for
iconv_open(char *toCode, char* fromCode), what would be "toCode" and
"fromCode" value? I think "toCode" will be UTF8, but what would be
"fromCode"?


Thanks and regards,
- Uday

0
Reply uday.sen (8) 6/6/2006 10:22:44 AM

uday.sen@gmail.com wrote:
> Hi,
> 
> I need to convert a string from UTF8 to wide character (wchar_t *). I
> perform the same in windows using:
> 
>      MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen);
> 
> However, in linux this API is not available. However, there exists
> mbstowcs() API, which converts multibyte string to wide character. But
> will this API convert UTF8 encoded string to wide character? Or this
> API will convert *only ASCII* characters to wide characters?
> 
> There exists also iconv() API which converts characterset using a
> characterset conversion descriptor returned by iconv_open(). But for
> iconv_open(char *toCode, char* fromCode), what would be "toCode" and
> "fromCode" value? I think "toCode" will be UTF8, but what would be
> "fromCode"?
> 
> 
> Thanks and regards,
> - Uday
> 
Your fromCode is UTF8 and your toCode is WCHAR_T.

Robert
0
Reply robert.f.harris (386) 6/6/2006 11:03:00 AM


You need to set tocode to "UTF-8" and fromcode to "WCHAR_T"
To get a complete list for to and from do 'iconv --list'

uday.sen@gmail.com wrote:
> Hi,
>
> I need to convert a string from UTF8 to wide character (wchar_t *). I
> perform the same in windows using:
>
>      MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen);
>
> However, in linux this API is not available. However, there exists
> mbstowcs() API, which converts multibyte string to wide character. But
> will this API convert UTF8 encoded string to wide character? Or this
> API will convert *only ASCII* characters to wide characters?
>
> There exists also iconv() API which converts characterset using a
> characterset conversion descriptor returned by iconv_open(). But for
> iconv_open(char *toCode, char* fromCode), what would be "toCode" and
> "fromCode" value? I think "toCode" will be UTF8, but what would be
> "fromCode"?
> 
> 
> Thanks and regards,
> - Uday

0
Reply goodithink (2) 6/6/2006 11:23:10 AM

Oops .. sorry for my foolish swap in my previous post ..

Set fromcode to "UTF-8" and tocode to "WCHAR_T"

ithink wrote:
> You need to set tocode to "UTF-8" and fromcode to "WCHAR_T"
> To get a complete list for to and from do 'iconv --list'
>
> uday.sen@gmail.com wrote:
> > Hi,
> >
> > I need to convert a string from UTF8 to wide character (wchar_t *). I
> > perform the same in windows using:
> >
> >      MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen);
> >
> > However, in linux this API is not available. However, there exists
> > mbstowcs() API, which converts multibyte string to wide character. But
> > will this API convert UTF8 encoded string to wide character? Or this
> > API will convert *only ASCII* characters to wide characters?
> >
> > There exists also iconv() API which converts characterset using a
> > characterset conversion descriptor returned by iconv_open(). But for
> > iconv_open(char *toCode, char* fromCode), what would be "toCode" and
> > "fromCode" value? I think "toCode" will be UTF8, but what would be
> > "fromCode"?
> > 
> > 
> > Thanks and regards,
> > - Uday

0
Reply goodithink (2) 6/6/2006 11:25:07 AM

Thanks a lot for your prompt help. I have following 2 questions:

1. Will "WCHAR_T" be platform independent? Going forward I plan to
deploy the piece of code on Solaris 10.
2. When converting using iconv(), how can I determine the size required
for output string? "iconv(3)" man page does not tell about anything
about it.
         size_t retval = iconv(cd, pInput, wcslen(pInput), pOutput,
???);
Do I have to calculate it by myself? Or there is any platform API that
I can use for my purpose.

Thanks again,
- Uday

0
Reply uday.sen (8) 6/6/2006 12:07:50 PM

4 Replies
43 Views

(page loaded in 0.121 seconds)

Similiar Articles:




7/25/2012 10:14:44 PM


Reply: