Hex encoding

From Just Solve the File Format Problem
(Difference between revisions)
Jump to: navigation, search
 
Line 7: Line 7:
 
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.
 
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. Whitespace characters may be allowed, and ignored.
+
Both uppercase ("A" through "F") and lowercase ("a" through "f") letters are common, and hex decoders typically support both. Whitespace characters may be allowed, and ignored. Other characters that aren't hex digits might be used for special purposes.
  
 
The most significant hex digit almost always [[Bit order|comes first]], but variants in which the least significant digit comes first do exist, for example [[ASCII Encoded HP 48 Object]].
 
The most significant hex digit almost always [[Bit order|comes first]], but variants in which the least significant digit comes first do exist, for example [[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.
  
 
The earliest versions of [[BinHex]] used a form of hex encoding, but the program later evolved to a different encoding based on base 64 instead of base 16 (similar to [[Base64]] encoding but with a different set of characters).
 
The earliest versions of [[BinHex]] used a form of hex encoding, but the program later evolved to a different encoding based on base 64 instead of base 16 (similar to [[Base64]] encoding but with a different set of characters).
  
 
== Examples ==
 
== Examples ==
The hex encoding of the ASCII string "<code>hijkl</code>", followed by a newline character ("<code>\n</code>"), is "<code>68696A6B6C0A</code>".
+
<code>xxd</code> is a widely-installed Unix utility that can do hex encoding and decoding.
  
A [[perl]] one-liner to do hex encoding:
+
  $ echo "Hex encoding test" | xxd -p
  perl -ne 'print uc(unpack("H*",$_))' original.bin &gt; encoded.txt
+
48657820656e636f64696e6720746573740a
  
And decoding:
+
  $ echo "48657820656e636f64696e6720746573740a" | xxd -r -p
  perl -ne 'print pack("H*",$_)' encoded.txt &gt; decoded.bin
+
Hex encoding test
 +
 
 +
== Software ==
 +
* [http://linux.about.com/library/cmd/blcmdl1_xxd.htm xxd]
  
 
== Links ==
 
== Links ==

Latest revision as of 13:14, 17 October 2015

File Format
Name Hex encoding
Ontology

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.

Both uppercase ("A" through "F") and lowercase ("a" through "f") letters are common, and hex decoders typically support both. Whitespace characters may be allowed, and ignored. Other characters that aren't hex digits might be used for special purposes.

The most significant hex digit almost always comes first, but variants in which the least significant digit comes first do exist, for example 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.

The earliest versions of BinHex used a form of hex encoding, but the program later evolved to a different encoding based on base 64 instead of base 16 (similar to Base64 encoding but with a different set of characters).

[edit] Examples

xxd is a widely-installed Unix utility that can do hex encoding and decoding.

$ echo "Hex encoding test" | xxd -p
48657820656e636f64696e6720746573740a
$ echo "48657820656e636f64696e6720746573740a" | xxd -r -p
Hex encoding test

[edit] Software

[edit] Links

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox