s c h e m a t i c s : c o o k b o o k

/ StringChapter / Cookbook.StringRecipeConvertToFromASCII

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

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

The built in function char->integer converts a character to an extended ASCII value (i.e., a number in the range of 0 to 255), while integer->char takes an extended ASCII value and returns the corresponding character.

> (char->integer #\C)
67
> (integer->char 68)
#\D

The procedures char->latin-1-integer and latin-1-integer->char support conversions between characters in the platform-specific character set and platform-independent Latin-1 (ISO 8859-1) values:

> (char->latin-1-integer #\U)
85
> (latin-1-integer->char 241)
#\

Discussion

Character values are "extended ASCII" values from 0 to 255 (where the ASCII extensions are platform-specific). If integer->char is given an integer that is not in 0 to 255 inclusive, the exn:application:type exception is raised.

char->latin-1-integer returns the integer in 0 to 255 inclusive corresponding to the Latin-1 value for char, or #f if char (in the platform-specific character set) has no corresponding character in Latin-1.

latin-1-integer->char returns the character corresponding to the Latin-1 mapping of its input, or #f if the platform-specific character set does not support the corresponding Latin-1 character. If the input is not in 0 to 255 inclusive, the exn:application:type exception is raised.

For Unix and Mac OS, char->latin-1-integer and latin-1-integer->char are the same as char->integer and integer->char. For Windows, the platform-specific set and Latin-1 match except for the range #x80 to #x9F (which are unprintable control characters in Latin-1).

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
TopicOrder: 050

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, 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