21

I would like to convert a char* string to a wchar* string in C.

I have found many answers, but most of them are for C++. Could you help me?

Thanks.

7
  • 2
    What is the original encoding in your char*? UTF8? ANSI? What is the sizeof(wchar) on your system and what encoding does it rely upon? UCS-2 (16bit)? UCS-4 (32bit)? Commented Jan 28, 2011 at 8:23
  • @Benoit: Whoa... I thought sizeof(wchar) was always 2, no? Commented Jan 28, 2011 at 8:24
  • @Mehrdad: It is not necessarily 2. It is implementation-defined. If programming on Windows, it has a size of two bytes and holds UTF-16, with double wchar_t's for surrogate pairs. Commented Jan 28, 2011 at 8:25
  • @Benoit: o__O I did not know it's implementation-defined... interesting, thanks for the info. Commented Jan 28, 2011 at 8:26
  • It's on unix system, so i guess it doesn't matter no ? Commented Jan 28, 2011 at 10:22

5 Answers 5

30

Try swprintf with the %hs flag.

Example:

wchar_t ws[100]; swprintf(ws, 100, L"%hs", "ansi string"); 
Sign up to request clarification or add additional context in comments.

10 Comments

i will try this evening , for now i don't have access to a shell.Thanks
@NickDandoulakis I think this answer could be very useful, however I found out that swprintf could have 2 possible interfaces, could you please take a look at this question? stackoverflow.com/q/17716763/2436175
@Antonio the interface that requires the buffer length is the portable one.
@NickDandoulakis It won't compile on Mingw 4.5.2 for example, so unfortunately is not general!
this is a good solution when cross compiling to mingw on a linux platform
|
5

setlocale() followed by mbstowcs().

6 Comments

This is OK as long as the input is an ANSI string.
@Benoit: Yeah, there's obviously more to string conversion than calling just a single function. But I didn't give any details since I think this is all the OP's looking for...
The imput come from LdapDirectory, so i guess it's an UTF8 ?
@Benoit: There's no such thing as an "ANSI string". This will work if the original string is in the multibyte format corresponding to the currently set locale.
I already have found this function, but i can't use it correctly, i just want to encode a string to unicode to send in a mail subject header. Thanks to you
|
4

what you're looking for is

mbstowcs 

works just like the copy function from char* to char*

but in this case you're saving into a wchar_t*

Comments

0

If you happen to have the Windows API availiable, the conversion function MultiByteToWideChar offers some configurable string conversion from different encodings to UTF-16. That might be more appropriate if you don't care too much about portability and don't want to figure out exactly what the implications of different locale settings are to the string converison.

Comments

-4

if you currently have ANSI chars. just insert an 0 ('\0') before each char and cast them to wchar_t*.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.