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

Oscar Hellström <>
Fri Jun 5 18:30:39 CEST 2009


Hi Paul,

There is  no such thing as a char type in Erlang. A string is normally
represented by a list of integers, which represent ascii values.
Actually, with unicode, I guess they represent unicode code points... If
you want a "char" $a is the syntax for that. If you try to do Letter =
a, you will bind the variable Letter to the atom a, not the char. You
would like to do Letter = $a, and that's it :)

You can find the reference manual under "Erlang Programming" at
http://erlang.org/doc/, there you can look for section 2: "Data Types".

Best regards

Paul Johnston wrote:
> 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/
>
>  
>
>
>   


-- 
Oscar Hellström, 
Office: +44 20 7655 0337
Mobile: +44 798 45 44 773
Erlang Training and Consulting Ltd
http://www.erlang-consulting.com/



More information about the erlang-questions mailing list