8字符串解码为Windows

8字符串解码为Windows

本文介绍了将UTF-8字符串解码为Windows-1256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将UTF-8字符串编码为Windows-1256字符串:

I used this code to encode a UTF-8 string to Windows-1256 string:

        string q = textBox1.Text;
        UTF7Encoding utf = new UTF7Encoding();

        byte[] winByte = Encoding.GetEncoding(1256).GetBytes(q);

        string result = utf.GetString(winByte);

此代码有效,但是我无法解码结果或将其编码为原始字符串!
如何在转换之前将编码的字符串(结果变量)解码为相同的字符串(q变量)?

This code is working but I can't decode the result or encoded to original string!How I can decode an encoded string (result variable) to same before converted (q variable)?

推荐答案

看看下面的注释代码。注释说明了什么是错误的,以及如何正确进行操作,但是基本上发生的是:

Have a look at the commented code below. The comments explain what is wrong, and how to do it correctly, but basically what is happening is:

首先,您使用 Encoding.GetEncoding(1256 ).GetBytes(q)将字符串(即UTF16)转换为ANSI代码页1256字符串。

Firstly you use Encoding.GetEncoding(1256).GetBytes(q) to convert a string (which is UTF16) to an ANSI codepage 1256 string.

然后您使用UTF7编码转换回去。但这是错误的,因为您需要使用ANSI代码页1256编码将其转换回:

Then you use a UTF7 encoding to convert it back. But that's wrong because you need to use an ANSI codepage 1256 encoding to convert it back:

string q = "ABئبئ"; // UTF16.
UTF7Encoding utf = new UTF7Encoding(); // Used to convert UTF16 to/from UTF7

// Convert UTF16 to ANSI codepage 1256. winByte[] will be ANSI codepage 1256.
byte[] winByte = Encoding.GetEncoding(1256).GetBytes(q);

// Convert UTF7 to UTF16.
// But this is WRONG because winByte is ANSI codepage 1256, NOT UTF7!
string result = utf.GetString(winByte);

Debug.Assert(result != q); // So result doesn't equal q

// The CORRECT way to convert the ANSI string back:
// Convert ANSI codepage 1256 string to UTF16

result = Encoding.GetEncoding(1256).GetString(winByte);

Debug.Assert(result == q); // Now result DOES equal q

这篇关于将UTF-8字符串解码为Windows-1256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:37