What is @smithy/util-utf8?
@smithy/util-utf8 is a utility package for handling UTF-8 encoding and decoding in JavaScript. It provides functions to convert strings to UTF-8 byte arrays and vice versa, which can be useful in various scenarios such as data serialization, network communication, and file handling.
UTF-8 Encoding
This feature allows you to encode a string into a UTF-8 byte array. The `toUtf8` function takes a string as input and returns a Uint8Array representing the UTF-8 encoded bytes.
const { toUtf8 } = require('@smithy/util-utf8');
const utf8Bytes = toUtf8('Hello, world!');
console.log(utf8Bytes);
UTF-8 Decoding
This feature allows you to decode a UTF-8 byte array back into a string. The `fromUtf8` function takes a Uint8Array as input and returns the decoded string.
const { fromUtf8 } = require('@smithy/util-utf8');
const utf8Bytes = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
const decodedString = fromUtf8(utf8Bytes);
console.log(decodedString);