如何希伯来语(Unicode)的转换为ASCII在C#? [英] How to convert hebrew (unicode) to Ascii in c#?

查看:239
本文介绍了如何希伯来语(Unicode)的转换为ASCII在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立某种形式的,其中有数字和希伯来字母解码为ASCII文本文件。

I have to create some sort of text file in which there are numbers and Hebrew letters decoded to ASCII.

这是触发ButtonClick

This is file creation method which triggers on ButtonClick

protected void ToFile(object sender, EventArgs e)
{
    filename = Transactions.generateDateYMDHMS();
    string path = string.Format("{0}{1}.001", Server.MapPath("~/transactions/"), filename);
    StreamWriter sw = new StreamWriter(path, false, Encoding.ASCII);
    sw.WriteLine("hello");
    sw.WriteLine(Transactions.convertUTF8ASCII("שלום"));
    sw.WriteLine("bye");
    sw.Close();
}



你可以看到,我使用Transactions.convertUTF8ASCII()静态方法来转换大概从Unicode字符串从.NET到它的ASCII表示。我用它的短期希伯来语沙龙,回到????,而不是结果,我所需要的。

as you can see, i use Transactions.convertUTF8ASCII() static method to convert from probably Unicode string from .NET to ASCII representation of it. I use it on term Hebrew 'shalom' and get back '????' instead of result i need.

下面是方法。

public static string convertUTF8ASCII(string initialString)
{
    byte[] unicodeBytes = Encoding.Unicode.GetBytes(initialString);
    byte[] asciiBytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes);
    return Encoding.ASCII.GetString(asciiBytes);
}



而不必初始字解码为ASCII我得到的????在该文件中我创建即使我运行debbuger我得到同样的结果。

Instead of having initial word decoded to ASCII i get '????' in the file i create even if i run debbuger i get same result.

我在做什么错了?

推荐答案

您不能简单地任意Unicode字符为ASCII翻译。它可以做的最好的是丢弃不可支持的角色,因此, ???? 。显然,基本的7位字符将工作,但仅此而已。我很好奇,预期的结果是什么呢?

You can't simply translate arbitrary unicode characters to ASCII. The best it can do is discard the unsupportable characters, hence ????. Obviously the basic 7-bit characters will work, but not much else. I'm curious as to what the expected result is?

如果您需要这个的传输的(而不是代表),你可能会考虑基线64编码的基本UTF8字节。

If you need this for transfer (rather than representation) you might consider base-64 encoding of the underlying UTF8 bytes.

这篇关于如何希伯来语(Unicode)的转换为ASCII在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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