getUtf8Codec
Returns a codec for encoding and decoding UTF-8 strings.
This codec serializes strings using UTF-8 encoding. The encoded output contains as many bytes as needed to represent the string.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | Utf8CodecConfig | Optional configuration for the codec. |
Returns
VariableSizeCodec<string>
A VariableSizeCodec<string> for encoding and decoding UTF-8 strings.
Examples
Encoding and decoding a UTF-8 string.
Rejecting invalid UTF-8 instead of substituting the replacement character.
Decoding losslessly, preserving null characters and a leading byte order mark.
Remarks
By default, invalid UTF-8 is replaced with the replacement character (U+FFFD), a
leading byte order mark (U+FEFF) is stripped and null characters are stripped from
decoded strings, since they are commonly used as padding in fixed-size strings. Use the
fatal, ignoreBOM and removeNullCharacters options to change these behaviours. On
platforms whose TextDecoder does not implement the fatal option, the bytes are
validated by this package instead.
This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.
If you need a fixed-size UTF-8 codec, consider using fixCodecSize.
If you need a size-prefixed UTF-8 codec, consider using addCodecSizePrefix.
Separate getUtf8Encoder and getUtf8Decoder functions are available.