在C#中将字符转换为ASCII [英] Converting chars to ASCII in C#

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

问题描述

大家好,



我正在将一个旧的VB应用程序转换为C#,系统的一部分要求我将某些特殊字符转换为它们的ASCII等效字符。



在VB中,代码是:



Hi guys,

I'm converting an old VB application into C#, and part of the system requires me to convert certain special characters to their ASCII equivalent.

In VB, the code is:

sValue = Asc("œ")  'which gives 156

sValue = Asc("°")  'which gives 176

sValue = Asc("£")  'which gives 163





这些是根据 http: //www.ascii-code.com/





但是在C#中进行相同的转换时,第一次这些价值观给出了一个奇怪的答案。



以下是代码:





These are the correct values according to http://www.ascii-code.com/.


But when doing the same conversion in C#, the first of these values gives a strange answer.

Here is the code:

As ints:

int i1 = (int)Convert.ToChar("œ");    // which gives 339

int i2 = (int)Convert.ToChar("°");    // which gives 176

int i3 = (int)Convert.ToChar("£");    // which gives 163


As bytes:

byte i1 = (byte)Convert.ToChar("œ");    // which gives 83

byte i2 = (byte)Convert.ToChar("°");    // which gives 176

byte i3 = (byte)Convert.ToChar("£");    // which gives 163





是什么给出的?! :(我怀疑它与标志位有关,但我看不出是什么。



非常感谢



What gives?! :( I'm suspecting it's something to do with the sign bit, but I can't see what.

Many thanks

推荐答案

Richard是对的。要在C#中获取与VB中字节相同的字节,请使用:

Richard is right. To get the same bytes in C# as the bytes in VB, use this:
byte i1 = Encoding.Default.GetBytes("œ")[0];



GetBytes 方法返回一个字节数组, Encoding.Default.GetBytes(œ)[0] 你得到字节数组的第一个值。



希望这有帮助。


The GetBytes method returns a byte array, with Encoding.Default.GetBytes("œ")[0] you get the first value of the byte array.

Hope this helps.


Hello Nick,



你所谓的ASCII是*不是* ASCII(参见 http://en.wikipedia.org/wiki/ASCII [ ^ ])。

只有7位ASCII字符编码g是明确给出的。



原始的7位编码存在几个8位扩展。



您的页面声称列出 - 作为latin-1的一部分。但仔细阅读,页面上写着



[...]扩展的ASCII代码(字符代码128-255)

8位ASCII表有几种不同的变体。下表符合ISO 8859-1,也称为ISO Latin-1。 代码129-159包含Microsoft®WindowsLatin-1扩展字符。 [...]




微软决定一些几年前修改标准以满足他们的需求。请参见 http://www.cs.tut.fi/~jkorpela/chars.html [< a href =http://www.cs.tut.fi/~jkorpela/chars.htmltarget =_ blanktitle =New Window> ^ ]或更具体的http://www.cs.tut.fi/~jkorpela/chars.html#win [ ^ ]。



标准Latin-1 *不包含 - 。这包含在Latin-9(也称为ISO / IEC-8859-15)中,另请参见 ISO Latin 9与ISO Latin 1 相比[ ^ ]和 http://en.wikipedia.org/wiki/ISO/IEC_8859-15 [ ^ ]。



现在,如何解决你的问题?

latin-1和latin-9都不适用于Windows。

你需要带 Encoding.GetEncoding(1252)这与调用 Encoding.Default 的结果相同(如 ProgramFOX [ ^ ]在解决方案#3中描述。



干杯
Andi
Hello Nick,

What you refer to as being ASCII is *not* ASCII (see http://en.wikipedia.org/wiki/ASCII[^]).
Only the 7-bit ASCII character encoding is unambiguously given.

There exist several 8-bit extensions to the original 7-bit encoding.

Your page claims to list œ as being part of latin-1. But reding carefully, the page says

[...] The extended ASCII codes (character code 128-255)
There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1. Codes 129-159 contain the Microsoft® Windows Latin-1 extended characters. [...]


Microsoft decided some years ago to "modify" the standard to fit their needs. See http://www.cs.tut.fi/~jkorpela/chars.html[^] or more specific on http://www.cs.tut.fi/~jkorpela/chars.html#win[^].

Standard Latin-1 does *not* contain œ. That is included in Latin-9 (also known as ISO/IEC-8859-15), see also ISO Latin 9 as compared with ISO Latin 1[^] and http://en.wikipedia.org/wiki/ISO/IEC_8859-15[^].

Now, how to solve your issue?
Neither latin-1 nor latin-9 works on Windows.
You need to take Encoding.GetEncoding(1252) which happens to be the same result as calling Encoding.Default (as ProgramFOX[^] described in Solution #3).

Cheers
Andi


使用 GetBytes [ ^ ] Encoding.ASCII [ ^ ]编码以获取转换为ascii的字符。



祝你好运

Espen Harlinn
Use the GetBytes[^] of the Encoding.ASCII[^] encoding to get the characters converted to ascii.

Best regards
Espen Harlinn


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

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