Nicholas
Nicholas

Reputation: 135

Hexadecimals in haskell

I am wondering why when you define a hexadecimal and then use the formula "ord x - ord 'a' +10" Why is the +10 used? What would it give you if this +10 was not used?

Upvotes: 1

Views: 327

Answers (2)

sepp2k
sepp2k

Reputation: 370082

Without the + 10, you'd map the letters a to f to the value 0 to 5, instead of 10 to 15.

Upvotes: 6

mb14
mb14

Reputation: 22596

ord x - (ord 'a') gives you rank of the char 'a' being 0, 'b' 1 etc ... to convert this rank to the hexa value you need to add 10, so a => 10, b => 11 etc ..

You want 'a' to be 10 because after 9 (coded as '9') comes 10 (encoded as 'a').

Upvotes: 2

Related Questions