I am looking for code for convert unicode to 7bit ASCII. Any suggestions?
- 2Unicode is 16/32 bit - making it 7 bit is not decompression - it's compression.Amarghosh– Amarghosh2009-11-26 08:45:13 +00:00Commented Nov 26, 2009 at 8:45
- 3If you were to call it compression then it's lossy compression, at best.Jonas Elfström– Jonas Elfström2009-11-26 08:55:49 +00:00Commented Nov 26, 2009 at 8:55
- Do you need to "decompress" it, or do you want functions to print it and play with it?mrduclaw– mrduclaw2009-11-26 09:08:33 +00:00Commented Nov 26, 2009 at 9:08
- Unicode is actually 20 bits; UTF-16 is a 16 bits encoding and UTF-32 a (trivial) 32 bits encoding.MSalters– MSalters2009-11-26 09:40:37 +00:00Commented Nov 26, 2009 at 9:40
- yes i need function to decompress it in c++dweep– dweep2009-11-27 11:13:46 +00:00Commented Nov 27, 2009 at 11:13
| Show 4 more comments
3 Answers
If encoded with utf-8, it is the same for both ascii and unicode as ascii is a subset of unicode. See the example in RFC 2044
Comments
A simple example below:
try { System.IO.TextWriter writeFile = new StreamWriter("c:\\textwriter.txt",false,Encoding.UTF7); writeFile.WriteLine("example text here"); writeFile.Flush(); writeFile.Close(); writeFile = null; } catch (IOException ex) { MessageBox.Show(ex.ToString()); } 5 Comments
Xander
This allows you to write to UTF-7, the string can be in any encoding i believe
Xander
NOTE: If there are some characters in the string that is not supppored by UTF-7, it will be lost
Kylotan
Looks rather more Java than C++ to me.
mrduclaw
@Kylotan, I was thinking it looked more like C#, but same point: OP wanted a C++ answer.
MSalters
@Xander: UTF-7 is a Unicode encoding, like the UTF-16 used by .Net. Now there are characters outside of Unicode (Klingon for instance) but you can't lose those by converting between different Unicode representations.
I'd recommend to adapt the code from glib C function g_str_to_ascii() to C++: