October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Uint8Array toBase64() and toHex(): Convert Bytes Without btoa()

Use Uint8Array.toBase64() and toHex() on bytes directly, and use TextEncoder first when the input is Unicode text.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you already have bytes in a Uint8Array, convert them directly: call bytes.toBase64() for Base64 or bytes.toHex() for hexadecimal. That avoids creating an intermediate JavaScript string to make the bytes look like text for btoa(). If your input is Unicode text instead, encode it to bytes first with TextEncoder.

Convert an existing Uint8Array directly

The typed array already holds the byte values that Base64 and hex encode. For example:

As an Amazon Associate I earn from qualifying purchases.

const bytes = new Uint8Array([72, 101, 108, 108, 111]);

const base64 = bytes.toBase64();
const hex = bytes.toHex();

toBase64() returns a Base64 string; toHex() returns a hexadecimal string. The TC39 proposal demonstrates the bytes for “Hello World” as SGVsbG8gV29ybGQ= in standard Base64 and 48656c6c6f20576f726c64 in hex. See the TC39 proposal repository for the API examples and scope.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is a simpler representation path than turning each byte into a string character and passing that binary string to btoa(). The string in that older pattern is not ordinary text: its character values are being used to stand for bytes. The direct methods avoid that intermediate string; the available sources do not establish a quantified speed improvement.

Choose Base64 options for the receiving format

By default, toBase64() uses the standard Base64 alphabet, which includes + and /, and includes padding where required. If the destination expects URL-safe Base64 or no trailing padding, set those options explicitly:

const standard = bytes.toBase64();
const urlSafe = bytes.toBase64({ alphabet: "base64url" });
const unpadded = bytes.toBase64({ omitPadding: true });

Use the option that matches the format required by the protocol or receiving application; URL-safe characters and omitted padding are not interchangeable assumptions. MDN documents the alphabet and padding options.

Encode Unicode text as bytes first

Text encoding and Base64 encoding are separate operations. TextEncoder converts JavaScript text to UTF-8 bytes; toBase64() then encodes those bytes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const bytes = new TextEncoder().encode("Hello, world");
const base64 = bytes.toBase64();

This distinction matters with btoa(): it accepts a binary string whose characters each represent a single byte, not arbitrary Unicode text. Passing a character with a code point above 255 can throw an InvalidCharacterError. MDN explains the binary-string constraint and Unicode handling. When your source is text, choose the intended text encoding before Base64; when your source is already bytes, do not turn it into a string just to call btoa().

Decode Base64 back to bytes

For the reverse operation, Uint8Array.fromBase64() returns a byte array:

const decoded = Uint8Array.fromBase64(base64);

MDN documents options for alphabet selection and handling incomplete final chunks; the method ignores ASCII whitespace in the input. If you need to place decoded data in an array you have already allocated, setFromBase64() writes into that array. Check the fromBase64() reference for the decoding options and return behavior.

Use the view you intend to encode

A Uint8Array can cover only part of a larger ArrayBuffer. Calling toBase64() or toHex() on the view converts the bytes in that view, rather than requiring you to encode the entire backing buffer. This is useful when a byte sequence is a slice of a larger buffer; make sure you call the method on the intended view. The TC39 proposal documentation describes conversion on typed-array views.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check support and use the right runtime API

The methods are standardized: the TC39 proposal reached Stage 4 and is included in the standard. Standardization does not mean every older browser or pinned runtime implements them. MDN marks toBase64() and fromBase64() Baseline 2025, with support across the latest browser and device versions from September 2025, while warning that older environments may not support them. Treat that as a compatibility signal, not a guarantee for every embedded browser or server runtime. Check the oldest runtime in your support matrix before relying on the methods; if it lacks them, choose a compatible fallback or polyfill deliberately. See MDN’s notes for toBase64() and fromBase64().

For Node.js code built around Node APIs, use Buffer conventions. Node documents Base64 conversion with Buffer.from(value, 'base64') and buf.toString('base64'), and classifies buffer.atob() and buffer.btoa() as legacy APIs. See the Node.js Buffer documentation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.