Decode Hex

About Hexadecimal

What is hexadecimal?

Hexadecimal is a base-16 number system used to represent binary data in a human-readable form. Each byte is represented by two hex digits from 0-9 and a-f.

Why use hex?

Used hex when you need a simple, human-readable way to represent raw binary data as text. Avoid it when size is important, since hex doubles the data length compared to raw bytes or more compact encodings like base64.

Hex in Bash

# Encoding
printf 'Hello' | xxd -p
# Output: 48656c6c6f

# Decoding
printf '48656c6c6f' | xxd -r -p
# Output: Hello
			

Hex in JavaScript

// Encoding
let encoded = Buffer.from("Hello").toString("hex");
console.log(encoded);
// Output: 48656c6c6f

// Decoding
let decoded = Buffer.from("48656c6c6f", "hex").toString();
console.log(decoded);
// Output: Hello
			

Hex in Go

package main
import (
	"encoding/hex"
	"fmt"
)
func main() {
	// Encoding
	encoded := hex.EncodeToString([]byte("Hello"))
	fmt.Println(encoded)
	// Output: 48656c6c6f

	// Decoding
	decoded, _ := hex.DecodeString("48656c6c6f")
	fmt.Println(string(decoded))
	// Output: Hello
}
			

Hex in PHP

// Encoding
$encoded = bin2hex("Hello");
echo $encoded . "\n";
// Output: 48656c6c6f

// Decoding
$decoded = hex2bin("48656c6c6f");
echo $decoded . "\n";
// Output: Hello