Erlang Central

Difference between revisions of "String Convert To From ASCII"

From ErlangCentral Wiki

 
Line 1: Line 1:
 
 
== Problem ==
 
== Problem ==
  
Line 32: Line 31:
 
ok
 
ok
 
</code>
 
</code>
 +
 +
 +
[[Category:CookBook]]

Revision as of 21:29, 18 August 2006

Problem

You want to get the number representing the ASCII value of a character, or you want to display the characters associated with a particular ASCII numerical value.


Solution

All ASCII character values are identical to integers, and strings of characters are identical to lists of integers. Consequently, in most cases character values are already in the format you wish:

1> $C.
67
2> H = $C.
67
3> H.
67

Because integer values are the only character representations (and the $C notation is just a syntactic convenience), values that map to ASCII (or UNICODE) values may be used where characters are desired:

1> io:format("Number ~w is character ~c\n", [101, 101]).
Number 101 is character e
ok
2> io:format("~w~n", ["Sample"]).
[83,97,109,112,108,101]
ok
3> io:format("~s~n", [[83, 97, 109, 112, 108, 101]]).
Sample
ok
4> io:format("~s~n", [lists:map(fun(C) -> C+1 end, "HAL")]).
IBM
ok