[erlang-questions] Newbie question converting char to ascii value

Dale Harvey <>
Fri Jun 5 18:36:47 CEST 2009


char_to_ascii(Letter) ->
  case Letter of
    $a -> 97;
    $b -> 98
  end.

that would return exactly what you called it with,

when you say "take a character" where from,
if you want to pick the ascii character from a string
then you can just pull the list item,

>Letter = "a",
>hd(Letter).
97
> io:format("~w", ["abc"]).
[97,98,99]

might help

2009/6/5 Paul Johnston <>

> Hi
>
>
>
> I want to take a character store it in a variable and then get it's
> ascii value.
>
> I know
>
> 1> $a.
>
> 97
>
>
>
> But want something like
>
>
>
> 2> Letter = a.
>
> a
>
> 3> $Letter.
>
> * 1: syntax error before: etter
>
> 3>
>
>
>
> I could do it
>
>
>
> char_to_ascii(Letter) ->
>
>            case Letter of
>
>                        a -> 97;
>
>                        b -> 98;
>
>                        ...
>
>            end.
>
>
>
>
>
> But that would be silly :-)
>
>
>
> Cheers Paul
>
>
>
> Paul Johnston
>
> Humanities ICT (Infrastructure)
>
> Samuel Alexander Building
>
> Room W1.19
>
>
>
> e-mail 
>
> web http://web-1.humanities.manchester.ac.uk/prjs/mcasspj/
>
>
>
>


More information about the erlang-questions mailing list