我如何在C#中从ascii转换为unicode [英] How do i convert from ascii to unicode in C#

查看:86
本文介绍了我如何在C#中从ascii转换为unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个卢比符号,Unicode U + 20B9。

在我的记事本中,我使用快递新字体。

卢比符号在我们使用时会起作用卢比字体。



我计划转换Unicode,然后使用dos打印输出并运行.bat文件。



指导我。

I would like to print a Rupee symbol, Unicode U+20B9.
In my notepad I use courier new font.
The Rupee symbol will work when we use Rupee font.

I planned to convert Unicode and then take print out using dos and run .bat file.

Guide me.

推荐答案

您仍然需要卢比字体,请参阅:

http://en.wikipedia.org/wiki/Indian_rupee_sign [ ^ ],

http://techie-buzz.com/india-tech/ubuntu-10-10-indian-rupee-font.html [ ^ ]。



由于此角色点于2009年7月15日正式发布,并且最近才通过Unicode标准化,因此尚未集成到任何操作系统中(请参阅上面的链接) 。



至于DOS和ASCII - 忘了它。这部分问题毫无意义,可能基于对Unicode的误解。令人困惑的阅读在这里:

http://en.wikipedia.org/wiki/Unicode [ ^ ],

< a href =http://unicode.org/> http://unicode.org/ [ ^ ],

http://unicode.org/faq/utf_bom。 html [ ^ ]。







回复但是:



你应该明白,从Unicode到ASCII和ASCII到Unicode的转换的概念没有任何意义,因为与ASCII相比,Unicode ...不是编码。 (但是,它取决于你所谓的Unicode,因为在Windows行话中,术语Unicode通常用于Unicode转换格式(UTF)之一,UTF16LE。)Unicode是定义正式的标准 - 字符之间的对应关系被理解为从其精确图形中抽象出来的一些文化类别(例如,拉丁语A和西里尔语А是不同的字符,您可以通过使用此段落上的文本搜索来测试它)和整数,从他们的计算机演示文稿中抽象出来,如大小和结束。尽管存在共同的错误观点,但这不是16位代码,因为目前由Unicode标准化的代码点的范围远远超出了可以容纳16位的范围(称为基本多语言平面,BMP)。由于存在不同的整数类型,因此有几种不同的方式来表示称为UTF的Unicode文本。



Windows内部使用UTF16LE表示Unicode(是的,UTF16,以及UTF8和UTF32可以表示超出BMP的前16位的代码点,但是所有的API都很好地从这个事实中抽象出来。当字符数据序列化(未转换)为字节数组时,出现UTF。字符数据也可以序列化为ASCII,但某些信息可能会丢失,也可能不会丢失,因为ASCII的范围仅为0到127,字符点与Unicode中的含义相同。传统上,丢失的字符(超出ASCII范围的字符)被转换为?。作为字节数组的ASCII数据可以反序列化为字符数据(.NET字符串),当然,这总是没有损失。换句话说,ASCII码与Unicode的子集一一对应,代码点为0到127.



表示再说一遍:ASCII和Unicode之间没有转换这样的概念。



-SA
You still need the Rupee font, see:
http://en.wikipedia.org/wiki/Indian_rupee_sign[^],
http://techie-buzz.com/india-tech/ubuntu-10-10-indian-rupee-font.html[^].

As this character point was officially presented on 15 July 2009 and only recently standardized by Unicode, it wasn't integrated into any operating system yet (see the link above).

As to DOS and ASCII — just forget it. This part of question makes no sense and probably based on some misunderstanding of what Unicode is. De-confusing reading is here:
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].



In reply to "but":

you should understand that the whole notion of "conversion" from Unicode to ASCII and ASCII to Unicode makes no sense because Unicode… is not encoding, in contrast to ASCII. (However, it depends on what do you call "Unicode", because in Windows jargon, the term "Unicode" is often used for one of the Unicode Transformation Formats (UTFs), UTF16LE.) Unicode is a standard which defines formal one-to-one correspondence between "characters" understood as some cultural categories abstracted from their exact graphics (for example, Latin "A" and Cyrillic "А" are different characters, you can test it by using text search on this paragraph) and integer numbers, abstracted from their computer presentation like size and endianess. Despite of common wrong opinion, this is not 16-bit code as the range of "code points" presently standardized by Unicode goes far beyond the range which can fit in 16 bits (called Base Multilingual Plane, BMP). As there are different integer types, there are several different ways to represent Unicode text called UTFs.

Windows internally represents Unicode using UTF16LE (and yes, UTF16, as well as UTF8 and UTF32 can represent code points beyond first 16 bits of BMP), but all the APIs are well abstracted from this fact. The UTFs appears when character data is serialized (not "converted") into array of bytes. Character data can also be serialized into ASCII, but some information may or may not be lost, because the range of ASCII is only 0 to 127, and the character points have the same "meaning" as in Unicode. Traditionally, the lost characters (those beyond ASCII range) are "converted" to '?'. ASCII data as a array of bytes can be deserialized into character data (.NET string) and, naturally, that always goes without losses. In other word, ASCII code has one-to-one correspondence with the subset of Unicode with code points 0 to 127.

That said, again: there is no such concept as "conversion" between ASCII and Unicode.

—SA


using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}


这篇关于我如何在C#中从ascii转换为unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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