Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UTF8 to Windows-1252

Tags:

c#

asp.net

utf-8

I have an asp.net GET webservice that takes in a URL parameter, runs some logic, and then passes back a þ delimited list of values.

The problem I have is the service making the request is using Windows-1252 encoding, so the þ character doesn't display properly when it's returned to the requesting machine. I'm looking for a quick way to convert a string from UTF8 to Windows-1252 to pass back.

like image 493
user3486740 Avatar asked Jan 18 '26 09:01

user3486740


1 Answers

Convert your string inputStr to byte array :

byte[] bytes = new byte[inputStr.Length * sizeof(char)];
System.Buffer.BlockCopy(inputStr.ToCharArray(), 0, bytes, 0, bytes.Length);

Convert it to 1252:

Encoding w1252 = Encoding.GetEncoding(1252);
byte[] output = Encoding.Convert(utf8, w1252, inputStr);

Get the string back:

w1252.GetString(output);
like image 165
e4rthdog Avatar answered Jan 20 '26 21:01

e4rthdog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!