将UTF8转换为Windows-1252

问题描述:

我有一个asp.net GET网络服务,该服务接受一个URL参数,运行一些逻辑,然后传回þ分隔的值列表.

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.

我遇到的问题是发出请求的服务使用的是Windows-1252编码,因此当将þ字符返回到请求计算机时,properly字符不能正确显示.我正在寻找一种将字符串从UTF8转换为Windows-1252并传递回去的快速方法.

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.

将字符串inputStr转换为字节数组:

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);

将其转换为1252:

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

取回字符串:

w1252.GetString(output);