tags:

views:

101

answers:

4

The libc library I'm currently using is missing wctomb() so I'm looking to come up with a replacement implementation. What are some complexities I should beware of? Can I simply grab each byte in the wchar and stick them inside an char array?

A: 

It depends in what encoding is the wchar and what encoding do you expect in char array. If they are the same (the encodings), then AFAIK yes, you can do it byte by byte, but beware of endiannes.

Messa
A: 
JSBangs
UTF-8 encoding is not too hard, if you have characters already in Unicode (ie. in some other encoding of Unicode). (But it is still better to not reinvent the wheel and use some library.)
Messa
+2  A: 

You might want to pick up a copy of P.J. Plauger's book, "The Standard C Library" - it provides a basic implementation of wctomb() along with a discussion of wide character support in general.

Michael Burr
A: 

You can easily assign each wchar to a char if the wchar is <= 127 so only 7bit are used. As long as you check this and create an error if it isn't given you are save and can do so.

Thats because the first 7 bit og the unicode character (no matter if 8, 16 or 32 bit) is identical with the first 7 bit of ASCII.

If you really want to write a converter you need to know a lot of character tables and so on.

There is a full featured unicode library call ICU which was inspired by IBM. Its very powerful and big. But it might help if nothing helps.

Totonga