3

我一直在尝试解码以下字符串:

Crédit 

在 c# 中使用以下代码:

    Encoding iso = Encoding.GetEncoding("ISO-8859-1");
    Encoding utf8 = Encoding.UTF8;
    string msg = iso.GetString(utf8.GetBytes(@"Crédit"));

这是产生:

Crédit

我在网上查看 了http://jeppesn.dk/utf-8.html,这是正确的 utf 8,应该会产生:

Crédit

有人可以指出我哪里出错了吗?

谢谢

4

2 回答 2

4
于 2012-12-12T17:35:33.313 回答
2

You're trying to do something that doesn't make sense, basically. You should almost never1 be interpreting the output of one encoding as the input to another encoding. It's like saying, "Suppose I save this image as a gif... then load that file using a jpeg loader... what does it look like?"

I suspect that if you use:

// Just an example: don't actually do this.
string msg = utf8.GetString(iso.GetBytes(@"Crédit"));

... it will do what you want, but you shouldn't be doing this at all.

Now, what is your real input (in what form) and what are you trying to achieve?


1 If you're doing so, it's usually because someone else has already done the wrong thing, or there's a configuration problem somewhere. If you find yourself doing this, you should think very carefully about whether you should really be doing it, or whether you're just working around a different problem which should be tackled differently.

于 2012-12-12T17:38:43.743 回答