Xerobit

Random Hex Code Generator for Strings, Bytes and Colors

Use this random hex code generator to produce cryptographically secure hexadecimal strings, raw byte sequences or #RRGGBB color swatches. Choose the length, toggle the 0x prefix and generate bulk values instantly. Everything runs in your browser so nothing is uploaded anywhere.

Prepends 0x to each value.

Result

951960c1
  • Mode
    Hex string
  • Length
    8 digits
  • Prefix
    None
  • Case
    Lowercase
  • Generated
    1

How to use the random hex code generator

  1. Select Hex string, Bytes or Color from the mode dropdown.
  2. Enter the desired length in digits or bytes (ignored for color mode).
  3. Toggle the 0x prefix and uppercase options if needed.
  4. Set the count for bulk generation, then click Generate.
  5. Copy the results or use the color swatches directly in your CSS.

How the random hex code generator works

This random hex code generator uses the Web Crypto API (crypto.getRandomValues) to fill a typed array with unbiased random bytes. Each byte is converted to two hexadecimal characters. For example, generating a 4-byte sequence might produce the raw bytes [175, 12, 255, 0], which map to the hex string af0cff00. Because the source is a cryptographic pseudo-random number generator, every hex digit has an equal probability of appearing, making the output suitable for tokens, nonces and test fixtures.

In color mode the tool always generates exactly three bytes (six hex digits) and prepends a hash sign to form a valid CSS color like #3fa9c1.

String mode vs bytes mode

The tool offers two ways to control length, which matters when you use it as a random hex number generator for binary protocols or APIs.

ModeInputOutput lengthExample (length 4)
Hex stringN digitsN charactersa3f1
BytesN bytes2N charactersa3f1b209

Choose hex string when you need an exact character count, such as a 32-character session ID. Choose bytes when you are thinking in terms of memory sizes: 16 bytes equals 128 bits, which is a common key length. Both modes let you add the 0x prefix used in C, JavaScript and Python literals.

Using it as a random hex color generator

Switch to Color mode to use the tool as a random hex color generator. It produces standard #RRGGBB values that work in CSS, SVG, canvas and design software. When you generate multiple colors the results page shows a row of swatches so you can visually compare them before copying.

Bulk generation is useful for building palettes, populating chart libraries or creating placeholder styles. Set the count field to 20 or 50 and click Generate to get a full list instantly. Each value is a hash sign followed by six random lowercase or uppercase hex digits (three random bytes). The 0x prefix option does not apply in color mode.

Common lengths reference

Here are typical lengths developers look for when they need a random number generator hex output:

Use caseModeLengthTotal hex digits
Short token / nonceString88
UUID without dashesBytes1632
AES-128 keyBytes1632
AES-256 keyBytes3264
SHA-256 digestBytes3264
CSS colorColor3 bytes6

When working with other data formats you may also need to convert between representations. Tools like the Base64 encoder or binary to text converter on Xerobit pair well with hex output for encoding pipelines.

Security and randomness notes

JavaScript's Math.random() is not suitable for security-sensitive values because its algorithm varies across engines and is predictable after enough observations. This tool avoids that pitfall entirely by calling crypto.getRandomValues(), which draws from the operating system's entropy pool (such as /dev/urandom on Linux or BCryptGenRandom on Windows).

Even with a strong source, remember that a hex string's strength depends on its length. An 8-digit hex string has 168 (about 4.3 billion) possible values, which is fine for a short-lived CSRF token but too small for a long-term secret. Use at least 32 hex digits (16 bytes) for anything that must resist brute-force guessing.

Worked examples

ExampleInputResult
32-digit hex tokenMode: string; Length: 32; Add 0x prefix: no; Uppercase A-F: no; How many: 1Mode: Hex string; Length: 32 digits; Prefix: None; Case: Lowercase; Generated: 1
Four random bytes with 0xMode: bytes; Length: 4; Add 0x prefix: yes; Uppercase A-F: yes; How many: 1Mode: Bytes; Length: 4 bytes; Prefix: 0x; Case: Uppercase; Generated: 1
Five random colorsMode: color; Length: 6; Add 0x prefix: no; Uppercase A-F: no; How many: 5Mode: Color; Length: 6 digits (3 bytes); Prefix: None; Case: Lowercase; Generated: 5

FAQ

Is this random hex code generator cryptographically secure?

Yes. It uses the Web Crypto API's getRandomValues method rather than Math.random(), providing unpredictable output suitable for tokens, keys and nonces.

What is the difference between string mode and bytes mode?

String mode lets you specify the exact number of hex digits. Bytes mode lets you specify the number of raw bytes, producing twice as many hex digits since each byte is two characters.

Can I use this as a random hex color generator for CSS?

Yes. Select Color mode to generate standard #RRGGBB values. You can create multiple colors at once and preview them with the built-in swatches.

How do I add a 0x prefix to the output?

Check the "Add 0x prefix" option before clicking Generate. The prefix is added to every value, which is useful for JavaScript, C and Python numeric literals.

What is the maximum length I can generate?

You can generate up to 4,096 digits in string mode or 4,096 bytes (8,192 digits) in bytes mode. For most applications, 64 to 128 digits provide more than enough entropy.

Does this work offline?

The tool runs entirely in your browser with no network requests. Once the page is loaded, you can generate values even without an internet connection.