将Char转换为Int

21 int haskell char

我想知道如何将Char转换为Int.例如

a = '\x2' -- a == 2
          -- type of a should be Char
b = charToInt a -- b == 2
                -- type of b should be Int
Run Code Online (Sandbox Code Playgroud)

但我不知道如何:/

提前致谢

Chr*_*ich 25

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

chr 走向另一个方向.

> ord '\x2'­
  => 2
> chr 97
  => 'a'
> ord (chr 42)
  => 42
Run Code Online (Sandbox Code Playgroud)

  • 只是要添加,你必须`导入Char`或`import Data.Char`才能使用它们 (24认同)

aug*_*tss 5

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

  • @jpredham 我们有来自 `Data.Char` 的 `digitToInt '9'` 来对应这个确切的 puprose (3认同)