C#转换编码字符串IÜÜæØÜÜ?E?到可读的阿拉伯语 [英] C# Converting encoded string IÜÜæØÜÜ?E? to readable arabic
问题描述
我需要在WinForm中正确显示可读的阿拉伯字符串,在Notepad ++中,我将编码设置为ANSI,粘贴字符串ÌÜÜæØÜÜÇÈáÊíÌÜÜí2",然后转到编码-> Char编码->阿拉伯-> Windows 1256,然后我得到正确的值جــوطــابل تيجــي 2
I need to show readable arabic string correctly in WinForm, in Notepad++ i set encoding to ANSI, paste the string "ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2", after that i go to encoding -> Char coding -> Arab -> Windows 1256, and i gets the correct value جــوطــابل تيجــي 2
我如何通过C#做到这一点?,我尝试这样做但不起作用:
How can i do that by C#?, i try with that but dosn't work:
Encoding en_source = Encoding.GetEncoding("windows-1250");
Encoding en_dest = Encoding.Unicode;
byte[] srcBytes = en_source.GetBytes("ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2");
return en_dest.GetString(srcBytes);
推荐答案
您要从代码页1252转换为1256.
You want to convert from codepage 1252 to 1256.
var oldStr = "ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2";
var enSource = Encoding.GetEncoding(1252); // Windows 1252
var enDest = Encoding.GetEncoding(1256); // Windows 1256
var srcBytes = enSource.GetBytes(oldStr);
var newStr = enDest.GetString(srcBytes);
请注意,这在这种情况下可行,但是您可能还有其他情况,无法使用 enSource
作为代码页1252正确转换.假设此废话字符串在旧应用程序中以相同的方式生成,应该适合您.
Note that this works in this case, but you may have other cases that does not convert correctly using enSource
as codepage 1252. Assuming this nonsense string is generated the same way in your old application, this should work fine for you.
这篇关于C#转换编码字符串IÜÜæØÜÜ?E?到可读的阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!