将Char转换为Int

时间:2011-03-08 00:02:18

标签: haskell char int

我想知道如何将Char转换为Int。 E.g。

a = '\x2' -- a == 2
          -- type of a should be Char
b = charToInt a -- b == 2
                -- type of b should be Int

但我不知道如何:/

提前致谢

2 个答案:

答案 0 :(得分:23)

您可以使用ord函数将字符转换为整数(序数)表示。

chr走另一个方向。

> ord '\x2'­
  => 2
> chr 97
  => 'a'
> ord (chr 42)
  => 42

答案 1 :(得分:4)

您可以使用fromEnum或Data.Char.ord。

相关问题