从查询字符串中解码大写字母
Decoding capital letters from query string
QueryString 包含 foo=Þórþ Örnö Ægirssonð
当我将其解码为 UTF8
时,它适用于 ð, ö, æ
但不适用于大写版本 Ð, Ö, Þ
byte[] bytes = Encoding.Default.GetBytes(Request.QueryString["foo"]);
var value = Encoding.UTF8.GetString(bytes);
下面是这个字符串在解码之前的样子 Ãórþ Ãrnö Ãgirssonð
这是解码为 UTF8 后的样子�?órþ �?rnö �?girssonð
我错过了什么吗?我已经尝试解码为 UTF7 以及我在 MSDN Encoding Class 中找到的任何适用于冰岛语的集合。
问题是您(可能)使用两种不同的编码:Default
用于生成字节数组,UTF8
用于重建字符串。很可能您的 PC 上的 Default
与 UTF8
不同,这会把事情搞砸。
使用Encoding.Default
时一定要小心。这是它的工作原理(直接引用自 MSDN):
Different computers can use different encodings as the default, and
the default encoding can even change on a single computer. Therefore,
data streamed from one computer to another or even retrieved at
different times on the same computer might be translated incorrectly.
In addition, the encoding returned by the Default property uses
best-fit fallback to map unsupported characters to characters
supported by the code page. For these two reasons, using the default
encoding is generally not recommended. To ensure that encoded bytes
are decoded properly, you should use a Unicode encoding, such as
UTF8Encoding or UnicodeEncoding, with a preamble. Another option is to
use a higher-level protocol to ensure that the same format is used for
encoding and decoding.
在这种情况下,UTF8
编码应该是您要寻找的编码,但您必须连贯地应用它:
Byte[] bytes = Encoding.UTF8.GetBytes(Request.QueryString["foo"]);
String value = Encoding.UTF8.GetString(bytes);
看看 this demo,它对我来说很好。
QueryString 包含 foo=Þórþ Örnö Ægirssonð
当我将其解码为 UTF8
时,它适用于 ð, ö, æ
但不适用于大写版本 Ð, Ö, Þ
byte[] bytes = Encoding.Default.GetBytes(Request.QueryString["foo"]);
var value = Encoding.UTF8.GetString(bytes);
下面是这个字符串在解码之前的样子 Ãórþ Ãrnö Ãgirssonð
这是解码为 UTF8 后的样子�?órþ �?rnö �?girssonð
我错过了什么吗?我已经尝试解码为 UTF7 以及我在 MSDN Encoding Class 中找到的任何适用于冰岛语的集合。
问题是您(可能)使用两种不同的编码:Default
用于生成字节数组,UTF8
用于重建字符串。很可能您的 PC 上的 Default
与 UTF8
不同,这会把事情搞砸。
使用Encoding.Default
时一定要小心。这是它的工作原理(直接引用自 MSDN):
Different computers can use different encodings as the default, and the default encoding can even change on a single computer. Therefore, data streamed from one computer to another or even retrieved at different times on the same computer might be translated incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these two reasons, using the default encoding is generally not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding, with a preamble. Another option is to use a higher-level protocol to ensure that the same format is used for encoding and decoding.
在这种情况下,UTF8
编码应该是您要寻找的编码,但您必须连贯地应用它:
Byte[] bytes = Encoding.UTF8.GetBytes(Request.QueryString["foo"]);
String value = Encoding.UTF8.GetString(bytes);
看看 this demo,它对我来说很好。