Explain Codes LogoExplain Codes Logo

Uint8array to string in JavaScript

javascript
polyfill
textdecoder
buffer
Alex KataevbyAlex Kataev·Mar 8, 2025
TLDR

To quickly convert a Uint8Array to a string, use TextDecoder. Smashing CPU hogs with style!

const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); console.log(new TextDecoder().decode(uint8Array)); // Outputs: "Hello" ... just like your program saying "Hi!"

This nifty method decodes binary data to a string supporting various encodings.

Quick fix with polyfill for encoding

Your environment is playing hard to get and doesn't support TextDecoder? No worries, the stringencoding library has your back. This polyfill will ensure your program runs like clockwork on any platform. Talk about being adaptable!

Making UTF-16 Unicode characters behave

We all know that 16-bit Unicode characters can be tricky, right? They’re like a tricky puzzle, but TextDecoder is your secret weapon:

const uint8Array = new Uint8Array([226, 130, 172]); console.log(new TextDecoder('utf-8').decode(uint8Array)); // Outputs: "€", now you’re talking in Euros!

This TextDecoder method is CPU-efficient and considered the best practice for decoding strings.

NodeJS to the rescue

For my NodeJS folks, your global Buffer class has got you covered to convert a Uint8Array to a string with no sweat:

const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); const buffer = Buffer.from(uint8Array); console.log(buffer.toString()); // Outputs: "Hello", Buffer class making introductions!

Decoding UTF-8, express lane style

Who likes to wait? Decoding character by character is painfully slow. Efficient decoding methods like in TextDecoder will get you there faster than an express elevator!

Handling variety of encodings

Because variety is the spice of life, TextDecoder lets you play with different encoding formats like utf-16le' or ISO-8859-1. Keep in mind that the correct encoding will help TextDecoder` translate your byte sequence into meaningful symbols.

Binary data: handle with care

Binary data need extra care to make sure the encoding of the Uint8Array and TextDecoder match. It's like making sure you're using the same language to prevent any embarrassing misunderstanding.

Efficiency with large datasets

Efficiency comes in many forms. When handling large datasets, strive for streaming data or processing it in chunks, it keeps your memory footprint low and your program snappy.

Practical testing and usage

With JsFiddle demos, you can test these concepts until you become a Uint8Array to string conversion guru.

Browser folks, there's no need to feel left out! The feross/buffer module adapts NodeJS-style Buffer to the browser.

Converting with different encodings

When handling Buffers or Uint8Arrays, be mindful of your encoding. Whether it's utf8, hex, or base64, your chosen encoding can have a big impact on your string output.

NodeJS Buffer from ArrayBuffer

Sometimes, an extra conversion step might be needed:

const uint8Array = new Uint8Array([72, 101, 108, 108, 111]); const buffer = Buffer.from(uint8Array.buffer); console.log(buffer.toString()); // Outputs: "Hello", the long road to say Hi!

There you have it, an extra trick up your sleeve!