Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › Uint8Array.toHex()

ECMAScript 2026

⚠️ Baseline Newly Available

Uint8Array.prototype.toHex()

Encode binary data as a hex string. Pairs with fromHex for roundtrips.

Last updated: October 2026

Definition: uint8Array.toHex() returns a hexadecimal string representation of the data. Each byte becomes exactly two hex characters. Pairs with Uint8Array.fromHex() for lossless roundtrips [citation:1][citation:20].

🎬 Live Example

Five common toHex patterns. Click any button to see the output.

Input:

                
Output:

            

💡 toHex is 2 chars per byte — human-readable but larger than base64.

📝

Basic Syntax

uint8Array.toHex()

// Returns: lowercase hexadecimal string
// 2 characters per byte
// Inverse: Uint8Array.fromHex(hexString)

🎯 Parameters & Return

Parameter Type Description
——Takes no arguments.
ReturnstringLowercase hex representation of the bytes.
1

Basic Encoding

const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
bytes.toHex();
// "48656c6c6f"

// Same as "Hello" in UTF-8
2

Roundtrip with fromHex

const original = new Uint8Array([1, 2, 255]);

const hex = original.toHex();
// "0102ff"

const decoded = Uint8Array.fromHex(hex);
// Uint8Array([1, 2, 255])

// Lossless — data preserved

Pro tip: fromHex is the exact inverse of toHex. Perfect for roundtrips.

3

Binary Data Representation

const bytes = new Uint8Array([0, 1, 15, 16, 255]);
bytes.toHex();
// "00010f10ff"

// Each byte = exactly 2 hex chars
// 0x00 → "00", 0xff → "ff"
4

Hash Display

// Display a cryptographic hash
const bytes = crypto.getRandomValues(new Uint8Array(8));
const hex = bytes.toHex();
// "a3f1c9b8d4e2f761"

// Fixed length — predictable output
5

Real-World: Compare Bytes

const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([1, 2, 4]);

a.toHex() === b.toHex();
// false

// Quick equality check via hex strings

🎯 toHex vs toBase64 vs toString

Method Size per byte Use when
toHex2 charsReadable, predictable output
toBase64~1.33 charsCompact transport (URLs, tokens)
toStringVariesDebugging (comma-separated)

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does Uint8Array.toHex() do in JavaScript?

Uint8Array.toHex() returns a hexadecimal string representation of the data in the Uint8Array. Each byte becomes exactly two hex characters.

What is the difference between toHex and toBase64?

toHex produces 2 characters per byte (readable but larger). toBase64 produces about 4 characters per 3 bytes (compact). Use hex for debugging, base64 for transport.

When was Uint8Array.toHex() added to JavaScript?

Uint8Array.toHex() and fromHex() were approved as part of ECMAScript 2026 (June 2026). They reached Baseline Newly Available in 2026.

How do I decode a hex string back to bytes?

Use Uint8Array.fromHex(hexString). It is the inverse of toHex() and is also part of ECMAScript 2026.

Does toHex() modify the original Uint8Array?

No. toHex() returns a new string. The Uint8Array is unchanged.

🎯 What's Next?

← All JavaScript Reference