Auto-format: Spaces and 0x prefix will be removed

About Hex to Base64

Hexadecimal (Hex) is a numeral system using 16 symbols (0-9 and A-F). Converting hex to Base64 is useful for:

  • Representing binary data in text format
  • Data transmission and storage
  • Encoding cryptographic hashes
  • APIs and web services

How to Convert Hex to Base64

  1. 1Paste or type your hexadecimal string into the input field.
  2. 2The tool automatically validates the hex format (supports with/without 0x prefix).
  3. 3View the Base64 encoded result instantly.
  4. 4Copy the result or download it for further use.

Code Examples

JavaScript

// Convert hex string to Base64
function hexToBase64(hexString) {
  // Remove 0x prefix if present
  const hex = hexString.replace(/^0x/, '');

  // Convert hex to bytes
  const bytes = new Uint8Array(
    hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))
  );

  // Convert bytes to Base64
  let binary = '';
  bytes.forEach(byte => binary += String.fromCharCode(byte));
  return btoa(binary);
}

// Example usage
const hexString = '48656c6c6f20576f726c64';
const base64 = hexToBase64(hexString);
console.log(base64); // Output: SGVsbG8gV29ybGQ=

Python

import base64

def hex_to_base64(hex_string):
    # Remove 0x prefix if present
    hex_string = hex_string.replace('0x', '')

    # Convert hex to bytes
    byte_data = bytes.fromhex(hex_string)

    # Encode to Base64
    base64_data = base64.b64encode(byte_data)
    return base64_data.decode('utf-8')

# Example usage
hex_string = '48656c6c6f20576f726c64'
base64_result = hex_to_base64(hex_string)
print(base64_result)  # Output: SGVsbG8gV29ybGQ=

PHP

<?php
function hexToBase64($hexString) {
    // Remove 0x prefix if present
    $hexString = preg_replace('/^0x/', '', $hexString);

    // Convert hex to binary
    $binary = hex2bin($hexString);

    // Encode to Base64
    return base64_encode($binary);
}

// Example usage
$hexString = '48656c6c6f20576f726c64';
$base64Result = hexToBase64($hexString);
echo $base64Result;  // Output: SGVsbG8gV29ybGQ=
?>

Node.js

// Convert hex string to Base64
function hexToBase64(hexString) {
  // Remove 0x prefix if present
  const hex = hexString.replace(/^0x/, '');

  // Create buffer from hex
  const buffer = Buffer.from(hex, 'hex');

  // Convert to Base64
  return buffer.toString('base64');
}

// Example usage
const hexString = '48656c6c6f20576f726c64';
const base64 = hexToBase64(hexString);
console.log(base64); // Output: SGVsbG8gV29ybGQ=

Key Features

  • Automatic hex string validation
  • Support for 0x prefix (optional)
  • Flexible format handling (with/without spaces)
  • Byte representation preview
  • Instant conversion
  • Copy to clipboard functionality
  • Download converted result
  • Client-side processing (secure and private)

Common Use Cases

Cryptography

Convert cryptographic hashes and keys from hex format to Base64 for API usage.

Data Encoding

Transform hex-encoded data to Base64 for storage or transmission.

API Integration

Convert hex values to Base64 when interfacing with APIs that require Base64 input.

Web Development

Convert hex color codes or binary data to Base64 for embedding in HTML/CSS.

Frequently Asked Questions

What is hexadecimal encoding?

Hexadecimal (hex) is a base-16 number system that uses digits 0-9 and letters A-F. Each hex digit represents 4 bits, so two hex digits represent one byte. It is commonly used in computing to represent binary data in a human-readable format.

Can I include the 0x prefix in my hex string?

Yes, our tool automatically handles the 0x prefix. You can input hex strings with or without it, and the converter will process them correctly.

What if my hex string has spaces or separators?

The tool is flexible and can handle hex strings with spaces, colons, or hyphens as separators. It will automatically remove these before conversion.

Is there a limit to the hex string length?

While there is no strict limit, extremely long hex strings (megabytes of data) may slow down the browser. The tool is optimized for typical use cases.

Is my data secure?

Absolutely. All conversions happen locally in your browser using JavaScript. Your hex data is never uploaded to any server, ensuring complete privacy.