开发者

How to convert Char to Int in Haskell?

开发者 https://www.devze.com 2023-02-15 04:49 出处:网络
I n开发者_Python百科eed to convert a Char to an Int in Haskell? For example: a = \'\\x2\' -- a == 2

I n开发者_Python百科eed to convert a Char to an Int in Haskell? For example:

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

How can I achieve this?


You can use the ord function to convert a character to its integer (ordinal) representation.

chr goes the other direction.

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


You can use fromEnum or Data.Char.ord.

0

精彩评论

暂无评论...
验证码 换一张
取 消