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

Robert Virding rvirding@REDACTED
Sat Jun 6 00:16:45 CEST 2009


Actually as some one has already pointed out there is no character type in
erlang and $a is actually just syntactic sugar for 97. In this sense the
char_to_ascii function is really just the identity function. Strings are
just lists of integers.

Robert

2009/6/5 Dale Harvey <harveyd@REDACTED>

> 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 <paul.a.johnston@REDACTED>
>
> > 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 Paul.Johnston@REDACTED
> >
> > web http://web-1.humanities.manchester.ac.uk/prjs/mcasspj/
> >
> >
> >
> >
>


More information about the erlang-questions mailing list