What is @webassemblyjs/utf8?
@webassemblyjs/utf8 is a utility package for encoding and decoding UTF-8 strings in the context of WebAssembly. It provides functions to convert between UTF-8 byte arrays and JavaScript strings, which is useful when dealing with WebAssembly text format (WAT) or binary format (WASM).
Encode a string to UTF-8
This feature allows you to encode a JavaScript string into a UTF-8 byte array. The `encode` function takes a string as input and returns a Uint8Array representing the UTF-8 encoded bytes.
const { encode } = require('@webassemblyjs/utf8');
const utf8Array = encode('Hello, WebAssembly!');
console.log(utf8Array);
Decode a UTF-8 byte array to a string
This feature allows you to decode a UTF-8 byte array back into a JavaScript string. The `decode` function takes a Uint8Array as input and returns the corresponding string.
const { decode } = require('@webassemblyjs/utf8');
const utf8Array = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 101, 98, 65, 115, 115, 101, 109, 98, 108, 121, 33]);
const str = decode(utf8Array);
console.log(str);