Uint8Array to represent binary data. However, sometimes we need to convert this binary data to a readable string format. In this quick guide, we will explore how to convert
Converting Uint8Array to String
The easiest way to convert
Uint8Array to a string is to use the
TextDecoder API. This API is available in most modern browsers and can be used as follows:
const uint8array = new Uint8Array([72, 101, 108, 108, 111]); const decoder = new TextDecoder('utf-8'); const decodedString = decoder.decode(uint8array); console.log(decodedString); // Output: "Hello"
In the above example, we first create a
Uint8Array with the binary data representing the string "Hello". Then, we create a new
TextDecoder instance with the encoding set to 'utf-8'. Finally, we decode the
Uint8Array using the
decode() method of the
TextDecoder instance, which returns the decoded string.
TextDecoder API. By following the steps outlined in this quick guide, you can easily convert binary data to a readable string format.