Go to the previous, next section.

Internal Representation of Characters

An MIT Scheme character consists of a code part and a bucky bits part. The MIT Scheme set of characters can represent more characters than ASCII can; it includes characters with Super, Hyper, and Top bucky bits, as well as Control and Meta. Every ASCII character corresponds to some MIT Scheme character, but not vice versa.(6)

MIT Scheme uses a 7-bit ASCII character code with 5 bucky bits. The least significant bucky bit, Meta, is stored adjacent to the MSB of the character code, allowing the least significant 8 bits of a character object to be interpreted as ordinary ASCII with a meta bit. This is compatible with standard practice for 8-bit characters when meta bits are employed.

procedure+: make-char code bucky-bits

Builds a character from code and bucky-bits. Both code and bucky-bits must be exact non-negative integers in the appropriate range. Use char-code and char-bits to extract the code and bucky bits from the character. If 0 is specified for bucky-bits, make-char produces an ordinary character; otherwise, the appropriate bits are turned on as follows:

1               Meta
2               Control
4               Super
8               Hyper
16              Top

For example,

(make-char 97 0)                        =>  #\a
(make-char 97 1)                        =>  #\M-a
(make-char 97 2)                        =>  #\C-a
(make-char 97 3)                        =>  #\C-M-a

procedure+: char-bits char

Returns the exact integer representation of char's bucky bits. For example,

(char-bits #\a)                         =>  0
(char-bits #\m-a)                       =>  1
(char-bits #\c-a)                       =>  2
(char-bits #\c-m-a)                     =>  3

procedure+: char-code char

Returns the character code of char, an exact integer. For example,

(char-code #\a)                         =>  97
(char-code #\c-a)                       =>  97

variable+: char-code-limit

variable+: char-bits-limit

These variables define the (exclusive) upper limits for the character code and bucky bits (respectively). The character code and bucky bits are always exact non-negative integers, and are strictly less than the value of their respective limit variable.

procedure: char->integer char

procedure: integer->char k

char->integer returns the character code representation for char. integer->char returns the character whose character code representation is k.

In MIT Scheme, if (char-ascii? char) is true, then

(eqv? (char->ascii char) (char->integer char))

However, this behavior is not required by the Scheme standard, and code that depends on it is not portable to other implementations.

These procedures implement order isomorphisms between the set of characters under the char<=? ordering and some subset of the integers under the <= ordering. That is, if

(char<=? a b)  =>  #t    and    (<= x y)  =>  #t

and x and y are in the range of char->integer, then

(<= (char->integer a)
    (char->integer b))                  =>  #t
(char<=? (integer->char x)
         (integer->char y))             =>  #t

Note: if char is a character constant for which char->integer returns an integer strictly less than 256, then the compiler will constant-fold the call, replacing it with the corresponding integer. Likewise, if k is an integer constant strictly less than 256, the compiler will constant-fold a call to integer->char, replacing it with the corresponding character. This is a very useful way to denote unusual character constants or ASCII codes.

variable+: char-integer-limit

The range of char->integer is defined to be the exact non-negative integers that are less than the value of this variable (exclusive).

Go to the previous, next section.