e r l a n g : c o o k b o o k

/ Erlang.StringRecipeConvertToFromASCII

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Converting Between ASCII Characters and Values

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
Note: These examples are from Guillaume Cottenceau's helpful PLEAC site

Discussion

-- BrentAFulgham - 20 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
TopicOrder: 050

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest