本文介绍了在C#中将字符转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我正在将一个旧的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 156sValue = Asc("°") 'which gives 176sValue = 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 339int i2 = (int)Convert.ToChar("°"); // which gives 176int i3 = (int)Convert.ToChar("£"); // which gives 163As bytes:byte i1 = (byte)Convert.ToChar("œ"); // which gives 83byte i2 = (byte)Convert.ToChar("°"); // which gives 176byte 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推荐答案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. 这篇关于在C#中将字符转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 16:00
查看更多