Base64 to HEX Converter
Convert between Base64 and hexadecimal encoding
Base64 ⇄ HEX Converter
Convert between Base64 and hexadecimal encoding
Base64 to HEX Converter: Reconstructing Raw Byte Streams for Low-Level Analysis
In software development, cybersecurity, and digital communications, data encoded in Base64 frequently needs to be examined at the byte level. The Base64 to HEX Converter is a specialized developer utility that decodes standardized Base64 ASCII strings back into raw binary bytes and formats them into clean, human-readable hexadecimal (base-16) notation.
While Base64 (codified under IETF RFC 4648) is designed to safely serialize binary data across text-only protocols like HTTP, SMTP, JSON, and XML, it obscures the internal byte structure. Hexadecimal representation, on the other hand, provides direct visibility into individual 8-bit octets because each byte maps cleanly into two hexadecimal digits (spanning 00 through FF). Converting Base64 strings into hexadecimal is a standard workflow for security analysts, protocol engineers, and embedded systems developers who must inspect byte-level headers, cryptographic keys, magic file signatures, and binary payloads.
The Mathematical Decoding Process: 6-Bit to 8-Bit Reassembly
The conversion of Base64 to hexadecimal is the inverse of the Base64 encoding pipeline. It transforms sequences of 6-bit sextets back into standard 8-bit computational bytes:
- Sextet Retrieval: Every Base64 character represents a 6-bit value between 0 and 63. For example, the character 'S' corresponds to decimal 18 (binary
010010), while 'G' corresponds to decimal 6 (binary000110). - 24-Bit Bitwise Reassembly: Four consecutive Base64 characters yield twenty-four bits of information (
4 × 6 = 24 bits). The decoder shifts and concatenates these four sextets into a single 24-bit unsigned integer:(sextet_0 << 18) | (sextet_1 << 12) | (sextet_2 << 6) | sextet_3 - 8-Bit Byte Extraction: The 24-bit integer is sliced into three standard 8-bit octets by applying bitwise shift and mask operations:
- Byte 0 (high byte):
(integer >> 16) & 0xFF - Byte 1 (middle byte):
(integer >> 8) & 0xFF - Byte 2 (low byte):
integer & 0xFF
- Byte 0 (high byte):
- Hexadecimal Formatting: Each extracted 8-bit byte is translated into two hexadecimal characters by dividing by 16 (high nibble) and taking the remainder (low nibble).
Step-by-Step Worked Example: Decoding "SGlGaQ=="
Let us trace the complete mathematical decoding of the Base64 string "SGlGaQ==":
| Base64 Characters | Base64 Table Indices | 6-Bit Binary Sextets | Reconstructed 8-Bit Bytes | Hexadecimal Output |
|---|---|---|---|---|
| 'S', 'G', 'l', 'G' | 18, 6, 37, 6 | 010010, 000110, 100101, 000110 | 01001000 (72)01101001 (105)01000110 (70) | 48 69 46 |
| 'a', 'Q', '=', '=' | 26, 16, Pad, Pad | 011010, 010000, [Discard 4 bits] | 01101001 (105) | 69 |
Combining the resolved hexadecimal bytes yields: 48 69 46 69 (which translates to the ASCII string "HiFi").
Handling Trailing Padding Equals Signs ("=")
Base64 strings end with equals signs to ensure the total string length is an exact multiple of four:
- No Equals Sign: The data represented an exact multiple of three raw bytes. All four characters in each group produce valid bytes.
- One Equals Sign (
=): The final group of four characters contains only two actual data bytes (16 bits) followed by 2 padding zero bits. Exactly two hex bytes (4 hex digits) are generated. - Two Equals Signs (
==): The final group contains only one data byte (8 bits) followed by 4 padding zero bits. Exactly one hex byte (2 hex digits) is generated.
Decoding URL-Safe Base64 (base64url)
Standard Base64 contains characters (+, /, =) that cause conflicts when placed inside HTTP query strings, cookie headers, and REST URLs. The URL-Safe Base64 variant (RFC 4648 §5) substitutes these characters:
- The plus sign (
+) is replaced by a hyphen-minus (-). - The forward slash (
/) is replaced by an underscore (_). - Padding equals signs (
=) are frequently omitted entirely.
Our converter automatically recognizes and normalizes URL-safe inputs before decoding, ensuring seamless interoperability with modern JSON Web Tokens (JWT), Google Cloud APIs, and OAuth 2.0 credentials.
Programmatic Implementations in JavaScript and Python
Here is how software engineers implement Base64-to-HEX conversion programmatically:
1. JavaScript (Node.js & Modern Browser ES6+)
// Browser-compatible Base64 to Hex decoder
function base64ToHexBrowser(base64Str) {
// Normalize URL-safe characters
const normalized = base64Str.replace(/-/g, '+').replace(/_/g, '/');
const binary = atob(normalized);
const hexParts = [];
for (let i = 0; i !== binary.length; i++) {
const hex = binary.charCodeAt(i).toString(16).padStart(2, '0');
hexParts.push(hex.toUpperCase());
}
return hexParts.join(' ');
}
// Node.js Buffer implementation
function base64ToHexNode(base64Str) {
const normalized = base64Str.replace(/-/g, '+').replace(/_/g, '/');
return Buffer.from(normalized, 'base64').toString('hex').toUpperCase();
}
console.log(base64ToHexBrowser("SGlGaQ=="));
// Output: "48 69 46 69"2. Python 3
import base64
def base64_to_hex(base64_string: str) -> str:
# Normalize URL-safe inputs and restore missing padding
clean = base64_string.strip().replace('-', '+').replace('_', '/')
missing_padding = len(clean) % 4
if missing_padding:
clean += '=' * (4 - missing_padding)
raw_bytes = base64.b64decode(clean)
return raw_bytes.hex(' ').upper()
sample = "SGlGaSBUb29sa2l0"
print("Hex output:", base64_to_hex(sample))
# Output: "48 69 46 69 20 54 6F 6F 6C 6B 69 74"Real-World Cybersecurity and Engineering Applications
Converting Base64 data into hexadecimal notation is essential across multiple computing disciplines:
- Malware Deobfuscation & Reverse Engineering: Threat actors frequently encode malicious executable shellcode, PowerShell scripts, and DLL binaries as Base64 strings. Cybersecurity analysts decode these Base64 payloads into hexadecimal to inspect memory offsets, opcode instructions, and detect known malware signatures (YARA rules).
- Digital Forensics & Magic Byte Inspection: The first few bytes of any file format contain distinctive magic bytes (e.g.,
89 50 4E 47for PNG images,25 50 44 46for PDFs,4D 5Afor Windows PE executables). Decoding Base64 file attachments into hex allows investigators to verify file authenticity regardless of the file extension. - Cryptographic Key Inspection: Digital certificates (X.509 PEM), SSH keys, and asymmetric public keys stored in Base64 are decoded into hex to extract raw cryptographic curve coordinates (such as NIST P-256 or Curve25519 public keys).
- Network Protocol Debugging: Wireshark and tcpdump captures displaying Base64 authentication credentials (HTTP Basic Auth, NTLM headers) can be converted to hex to analyze raw socket transport boundaries.
Common Base64 Decoding Errors and Edge Cases
When processing automated data pipelines or manual developer inputs, several anomalies can cause Base64 decoding failures:
- Illegal Alphanumeric Characters: Characters outside the RFC 4648 alphabet (such as spaces, punctuation other than plus or slash, or accented glyphs) invalidate the base-64 mathematical stream. High-resilience decoders strip interior whitespace and line wraps before calculation.
- Truncated Bitstreams: A Base64 string whose non-padding character count modulo 4 equals one is mathematically impossible, as a single 6-bit sextet cannot form an 8-bit byte. When this occurs, decoders report buffer underflow or corrupted input warnings.
- Non-Zero Padding Bits: In strict compliance modes, the unused trailing bits in the final padded sextet must all be zeroes. If an attacker modifies these unused bits to smuggle hidden data, strict cryptographic validators reject the payload.
100% Client-Side Privacy and Zero-Latency Security Guarantee
Because Base64 strings frequently encode confidential data—such as cryptographic private keys, internal authentication tokens, confidential database records, or sensitive system binaries—security is of the utmost importance. Our Base64 to HEX Converter executes 100% locally within your web browser's JavaScript engine.
Not a single byte of your input data is ever sent across the network to external servers or logged in remote analytical databases. The tool operates with zero server latency, instant rendering, and complete offline capability, ensuring total data security and privacy.
Frequently Asked Questions (FAQs)
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
