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

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

问题描述

我在mdb文件中有一些编码数据,例如Úæäí和ÚáÇä;我尝试使用notepad ++,首先使用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);   

结果是عوني

一个警告,正如汉斯在评论中指出的: Windows-1252 有5个未使用的字节值( 0x81 0x8D 0x8F 0x90 0x9D ).如果这些字符与原始文本中使用的 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天全站免登陆