Hex encoding
(Created page with "{{FormatInfo |formattype=electronic |subcat=Transfer Encodings }} '''Hex encoding''' is a transfer encoding in which each byte is converted to the 2-digit base-16 encoding of ...") |
|||
Line 9: | Line 9: | ||
Most hex encoders use uppercase letters ("A" through "F"), but lowercase ("a" through "f") is not uncommon, and hex decoders typically support both. | Most hex encoders use uppercase letters ("A" through "F"), but lowercase ("a" through "f") is not uncommon, and hex decoders typically support both. | ||
− | The most significant hex digit almost always comes first, but variants in which the least significant digit comes first do exist. | + | The most significant hex digit almost always [[Bit order|comes first]], but variants in which the least significant digit comes first do exist, such as [[ASCII Encoded HP 48 Object]]. |
The distinction between hex encoding and ''base-16'' (hexadecimal) is not always clear. If you interpret a datastream as a single (probably huge) number, and write that number in base-16, it can, with suitable conventions, be equivalent to hex encoding. For example, consider the way that [[Encryption|cryptographic hashes]] are usually written. | The distinction between hex encoding and ''base-16'' (hexadecimal) is not always clear. If you interpret a datastream as a single (probably huge) number, and write that number in base-16, it can, with suitable conventions, be equivalent to hex encoding. For example, consider the way that [[Encryption|cryptographic hashes]] are usually written. |
Revision as of 00:53, 29 August 2014
Hex encoding is a transfer encoding in which each byte is converted to the 2-digit base-16 encoding of that byte (preserving leading zeroes), which is then usually encoded in ASCII. It is inefficient, but it is a simple, commonly-used way to represent binary data in plain text.
It is not a formal standard, so the term hex encoding may have other meanings, and the format described in the article may have other names.
Most hex encoders use uppercase letters ("A" through "F"), but lowercase ("a" through "f") is not uncommon, and hex decoders typically support both.
The most significant hex digit almost always comes first, but variants in which the least significant digit comes first do exist, such as ASCII Encoded HP 48 Object.
The distinction between hex encoding and base-16 (hexadecimal) is not always clear. If you interpret a datastream as a single (probably huge) number, and write that number in base-16, it can, with suitable conventions, be equivalent to hex encoding. For example, consider the way that cryptographic hashes are usually written.
Examples
The hex encoding of the ASCII string "hijkl
", followed by a newline character ("\n
"), is "68696A6B6C0A
".
A perl one-liner to do hex encoding:
perl -ne 'print uc(unpack("H*",$_))' original.bin > encoded.txt
And decoding:
perl -ne 'print pack("H*",$_)' encoded.txt > decoded.bin
Links
- Ten Minute Tutor: Hex (Base16) encoding
- Online Hex Encoder
- Online Hex Decoder
- RFC 4648: The Base16, Base32, and Base64 Data Encodings
- RFC 3548: The Base16, Base32, and Base64 Data Encodings (obsolete)