Skip to main content

char-int

char-int Function

Syntax:

char-int character → integer

Arguments and Values:

character—a character .

integer—a non-negative integer .

Description:

Returns a non-negative integer encoding the character object. The manner in which the integer is computed is implementation-dependent. In contrast to sxhash, the result is not guaranteed to be independent of the particular Lisp image.

If character has no implementation-defined attributes, the results of char-int and char-code are the same.

(char= c1 c2) (= (char-int c1) (char-int c2))

for characters c1 and c2.

Examples:

(char-int #\A)65 ; implementation A 
(char-int #\A)577 ; implementation B
(char-int #\A)262145 ; implementation C

See Also:

char-code

Expanded Reference: char-int

Basic usage

char-int returns a non-negative integer encoding of a character. The encoding is implementation-dependent.

(integerp (char-int #\A))
=> T
(>= (char-int #\A) 0)
=> T

Relationship to char-code

For characters with no implementation-defined attributes, char-int and char-code return the same value.

(= (char-int #\A) (char-code #\A))
=> T
(= (char-int #\Space) (char-code #\Space))
=> T

char= equivalence via char-int

Two characters are char= if and only if their char-int values are equal. This makes char-int a complete encoding that distinguishes all characters that char= can distinguish.

(= (char-int #\a) (char-int #\a))
=> T
(= (char-int #\a) (char-int #\A))
=> NIL
(= (char-int #\a) (char-int #\b))
=> NIL

Using char-int for hashing

Since char-int provides a unique integer for each distinct character, it can be useful for creating character-based hash computations.

(let ((chars "Hello"))
(reduce #'+ (map 'list #'char-int chars)))
;; => implementation-dependent