十六进制用两个 0-9、a-f 的基数 16 数字精确表示每个八位字节。编码器处理 UTF-8 或上传文件的字节并输出小写字符对;解码器移除空白后按字符对还原字节。
十六进制适合检查字节流、哈希、协议字段或文件签名,因为字节边界清晰可见。它会使原始体积翻倍,奇数个数字无效,而且本身不提供压缩、加密或完整性校验。
# Encoding
printf 'Hello' | xxd -p
# Output: 48656c6c6f
# Decoding
printf '48656c6c6f' | xxd -r -p
# Output: Hello
// 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
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
}
// Encoding
$encoded = bin2hex("Hello");
echo $encoded . "\n";
// Output: 48656c6c6f
// Decoding
$decoded = hex2bin("48656c6c6f");
echo $decoded . "\n";
// Output: Hello