【问题标题】:How to get hexadecimal number of a char?如何获得一个字符的十六进制数?
【发布时间】:2014-03-24 23:51:45
【问题描述】:

我必须开发一个Windows Mobile C# app 来比较字符并获取ascii 码

这是我的伪代码:

    public void getAscii(char c) 
    {
        int ascii_n = c.GetAscii(); //or something that gives me the hexadec number
        //You know; a=97, b=98...

        if (ascii_n > 12 && ascii_n < 23)
        { 
            //code lines
        }
        else if (ascii_n > 24 && ascii_n < 55)
        {
            //more code lines
        }
    }

知道我该怎么做吗??

【问题讨论】:

  • 非ASCII字符,即Unicode字符,你想做什么?
  • 没什么!我将只比较 a-zA-Z0-9
  • 所以,你想忽略'!`?
  • 请注意,此功能已经在框架中以Char.IsUpperChar.IsLowerChar.IsDigit 的形式提供。

标签: c# ascii


【解决方案1】:

您不需要 char 的十六进制表示,只需转换为整数就足够了

char c = '\n';
int ascii_n = (int)c;
Console.WriteLine(ascii_n);   // = 10

也适用于大于 255 的字符

char c = '∙';
int a = (int)c;
Console.WriteLine(a);   // = 8729

【讨论】:

  • 让我试试@Steve,非常感谢!
  • 这两个例子在技术上不是 Unicode 吗?只是 ASCII 的值与 Unicode 相同吗?
  • 是的,它们都是 unicode,这只是为了表明它也适用于高于 255 的值
猜你喜欢
  • 2012-03-24
  • 2021-12-22
  • 2013-08-20
  • 1970-01-01
  • 2015-04-03
  • 2014-03-19
  • 2018-10-30
  • 1970-01-01
相关资源
最近更新 更多