无法通过 C# 将 ANSI 编码复制到 Windows-1256 [英] Can't reproduce ANSI Encoding to Windows-1256 by C#

查看:21
本文介绍了无法通过 C# 将 ANSI 编码复制到 Windows-1256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mdb 文件中有一些编码数据,例如 Úæäí 和 ÚáÇä;我尝试使用记事本 ++,首先使用 ANSI 编码创建新文件,然后将该文本放入其中,最后将编码更改为 Windows-1256,结果是 عوني ,علان 完美,但我无法通过编码重现这种情况(C#).这是代码:

I have some encoded data in mdb file, like this Úæäí, and ÚáÇä; I tried with notepad++, first creating new file with ANSI Encoding, after that putting that text on to it, finally changing the encoding to Windows-1256, the result is عوني ,علان perfect, but i can't reproduce this scenario by coding(C#). here is the Code:

public string Decode(DataRow rw,string colName)
{
   Encoding srcEnc = Encoding.GetEncoding("from what ?");
   Encoding destEnc = Encoding.GetEncoding("1256");// arabic encoding
   byte[] srcVal = rscEnc.GetBytes(rw[colName].ToString());
   byte[] destVal = Encoding.Convert(srcEnc,destEnc,srcVal);
   return destEnc.GetString(destVal);
}

推荐答案

问题是您在编码之间转换.这实际上不是您想要实现的目标,您只是想重新解释编码的文本.

The problem is you're converting between encodings. This isn't actually what you're trying to achieve, you just want to re-interpret the encoded text.

为此,您需要获取 ANSI 字符串的字节,然后使用正确的编码对其进行解码.

To do this, you need to get the bytes for your ANSI string and then decode it using the correct encoding.

因此,省略转换:

var latin = Encoding.GetEncoding(1252);
var bytes = latin.GetBytes("Úæäí");

var arabic = Encoding.GetEncoding(1256);            
var result = arabic.GetString(bytes);   

result 是 عوني

警告,正如 Hans 在评论中指出的:Windows-1252 有 5未使用的字节值(0x810x8D0x8F0x900x9D>).如果这些对应于原始文本中使用的 Windows-1256 中的字符,那么您的源数据已损坏,因为这些字符将在使用 1252 的初始解码时丢失.理想情况下,您希望从原始编码源开始.

A caveat, as Hans points out in the comments: Windows-1252 has 5 byte values that are unused (0x81, 0x8D, 0x8F, 0x90, and 0x9D). If these correspond to characters in Windows-1256 used in the original text, then your source data is corrupted as these characters will have been lost on the initial decoding using 1252. Ideally, you want to start with the original encoded source.

这篇关于无法通过 C# 将 ANSI 编码复制到 Windows-1256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆