How to convert Char to Int in Haskell?

30,018

Solution 1

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

Solution 2

You can use fromEnum or Data.Char.ord.

Share:
30,018

Related videos on Youtube

Usama Abdulrehman
Author by

Usama Abdulrehman

Web Log data Processing Quick Prototyping for Processing Large Data-sets Installation & Configuration of Windows Server, SUSE Linux Enterprise Server, Ubuntu, CentOS Operating Systems on Physical Servers and Virtual Servers VMware & Oracle VM Virtual Box Deployment of Apache Hadoop, Sqoop, Hive, MySQL, Pig Build your systems using Apache Software Foundation Tools on top of most reliable and secure Linux Ecosystem. SAP Digital Badge Download in PDF Step by Step https://www.youtube.com/watch?v=UWH_HLtL9dI

Updated on July 09, 2022

Comments

  • Usama Abdulrehman
    Usama Abdulrehman almost 2 years

    I need 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?

    • Thomas M. DuBuisson
      Thomas M. DuBuisson about 13 years
      Discover and use hoogle.
  • newacct
    newacct about 13 years
    just to add, you have to import Char or import Data.Char in order to use those

Related