[Top] [Prev] [Next]


byte2char, char2byte - convert between bytes and characters


include "sys.m";
sys:= load Sys Sys->PATH;
byte2char: fn(buf: array of byte, n: int): (int, int, int);
char2byte: fn(c: int, buf: array of byte, n: int): int;


byte2char (buf, n)

The byte2char function converts a byte sequence to one Unicode character. The buf argument gives an array of bytes holding the sequence and n gives the number of bytes in the array. The returned tuple, (c, len, status), specifies the result of the translation:

The resulting Unicode character.


The number of bytes consumed by the translation.


Non-zero if the bytes are a valid UTF sequence and zero otherwise.

If the input sequence is not long enough to determine its validity, byte2char consumes zero bytes. If the input sequence is otherwise invalid, byte2char consumes one input byte and generates an error character (16r80, Sys->UTFerror), which prints (in most fonts) as a boxed question mark.

char2byte (c, buf, n)

The char2byte performs the inverse of byte2char. It translates a Unicode character, c, to a UTF byte sequence. The results are placed in buf, which should have at least n bytes available. The longest UTF sequence for a single Unicode character is Sys->UTFmax (3) bytes.

If the translation succeeds, char2byte returns the number of bytes placed in the buffer. If n bytes are too few to hold the result, char2byte returns zero and leaves the array unchanged.

See Also

Limbo System Modules, utfbytes, and utf


If the array bounds are invalid or insufficient to hold results, a run-time error occurs.

[Top] [Prev] [Next]

Copyright © 1996,Lucent Technologies, Inc. All rights reserved.