Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, November 8, 2015 1:23 PM
Hello all,
How can I convert from CP1252 to UTF8 and viseversa using C#??
Any ideas?
Thanks in advance
Motasim
All replies (5)
Sunday, November 8, 2015 2:17 PM ✅Answered
"What I'm trying to do is to convert a string in UTF8 to Windows-1252 or CP1252 and viseversa."
Problem is, there's no such thing as UTF8 or CP1252 string in C#/.NET, there's only UTF16 string. If you have text data stored in a byte array using a particular encoding then you could do something like this:
byte[] cp1252Text = new byte[] { 0xc4, 0x62, 0x63 };
Encoding cp1252 = Encoding.GetEncoding(1252);
string utf16String = cp1252.GetString(cp1252Text);
byte[] utf8Text = Encoding.UTF8.GetBytes(utf16String);
Monday, November 9, 2015 7:23 AM ✅Answered
"So, how I can convert the arabic string to the CP1252 string and vise-versa."
Encode the string to utf8 bytes and then decode the utf8 bytes as cp1252:
string utf16String = "اختبار جنسون";
byte[] utf8Text = Encoding.UTF8.GetBytes(utf16String);
Encoding cp1252 = Encoding.GetEncoding(1252);
string result = cp1252.GetString(utf8Text);
But that's a dubious thing to do. What do you need this anyway? Store Arabic text in a system that supports only 1252?!
Sunday, November 8, 2015 2:01 PM
What exactly are you trying to convert? Text files? Text data that's stored in byte arrays?
In general you can use the Encoding class for this kind of task. Note that you can't go directly from one encoding to another, you'll have to first convert from the source encoding to String (which implies UTF16) and then to the destination encoding.
Sunday, November 8, 2015 2:07 PM
Hi Mike,
What I'm trying to do is to convert a string in UTF8 to Windows-1252 or CP1252 and viseversa. Can you help me please by providing some code or something like that.
Thanks in advance
Motasim
Monday, November 9, 2015 7:01 AM
OK. Now let me ask this question. If I have a string in Arabic, and I want to get the CP1252, What do I need to do?
For example:
- The Arabic string: اختبار جنسون
- The CP1252 string is: اختبار جنسون
So, how I can convert the arabic string to the CP1252 string and vise-versa.
Thanks in advance
Motasim