C# 转换编码字符串 IÜæØÜÜ?E?可读的阿拉伯语 [英] C# Converting encoded string IÜÜæØÜÜ?E? to readable arabic

查看:25
本文介绍了C# 转换编码字符串 IÜæØÜÜ?E?可读的阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 WinForm 中正确显示可读的阿拉伯语字符串,在 Notepad++ 中,我将编码设置为 ANSI,粘贴字符串ÌÜÜæØÜÜÇÈá ÊíÌÜÜí 2",然后我转到编码 -> 字符编码 -> 阿拉伯语 -> 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屋!

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