hex

lightweight library for converting binary to hex and vice versa in a variety of formats

License

License

GroupId

GroupId

com.martiansoftware
ArtifactId

ArtifactId

hex
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

hex
lightweight library for converting binary to hex and vice versa in a variety of formats
Project URL

Project URL

https://github.com/martylamb/hex
Source Code Management

Source Code Management

https://github.com/martylamb/hex

Download hex

How to add to project

<!-- https://jarcasting.com/artifacts/com.martiansoftware/hex/ -->
<dependency>
    <groupId>com.martiansoftware</groupId>
    <artifactId>hex</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.martiansoftware/hex/
implementation 'com.martiansoftware:hex:0.1.0'
// https://jarcasting.com/artifacts/com.martiansoftware/hex/
implementation ("com.martiansoftware:hex:0.1.0")
'com.martiansoftware:hex:jar:0.1.0'
<dependency org="com.martiansoftware" name="hex" rev="0.1.0">
  <artifact name="hex" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.martiansoftware', module='hex', version='0.1.0')
)
libraryDependencies += "com.martiansoftware" % "hex" % "0.1.0"
[com.martiansoftware/hex "0.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.martiansoftware : hope-and-doubt jar 0.1.0

test (1)

Group / Artifact Type Version
junit : junit jar 3.8.1

Project Modules

There are no modules declared in this project.

martian-hex

Simple tools for encoding binary to hexadecimal and hexadecimal to binary.

Separate HexEncoder (for binary to hex) and HexDecoder (hex to binary) interfaces are provided, as well as HexCodec (both directions).

Several implementations are provided supporting several different hex formats, plus adapters for defining your own. Predefined HexCodecs can be found in StandardHexCodecs.java.

Static methods provided by Hex.java provide quick access to the StandardHexCodecs.DEFAULT HexCodec.

Encoding and Decoding

Encoding methods accept binary via byte arrays and InputStreams and can return Strings or encode directly to PrintStreams or PrintWriters. See HexEncoder.java for all encoding options.

Decoding methods accept hexadecimal in Strings or Readers and can return byte arrays or decode directly to OutputStreams. See HexDecoder.java for all decoding options.

Usage Examples

Bytes to Simple Hex

// byte array to string
byte[] b = "Here is an example.".getBytes();

System.out.println(Hex.encode(b)); // equivalent to StandardHexCodecs.DEFAULT.encode

Output: 4865726520697320616e206578616d706c652e

Simple Hex to Bytes

byte[] b = Hex.decode("4865726520697320616e206578616d706c652e");
System.out.println(new String(b)); // equivalent to StandardHexCodecs.DEFAULT.decode

Output: Here is an example.

Delimited by Colons, 16 bytes per line

String s = "Here is an example.";
StandardHexCodecs.SIMPLE.encode(s.getBytes(), System.out); // the same HexCodec can decode this back to binary as well

Output: 48:65:72:65:20:69:73:20:61:6e:20:65:78:61:6d:70 6c:65:2e

Hex Dump

byte[] b = new byte[67];
Random r = new Random();
r.nextBytes(b);
StandardHexCodecs.HEXDUMP.encode(b, System.out); // the same HexCodec can decode this back to binary as well

Output:

00000000: c9 85 90 e5 3c ca 13 72   82 52 5b c9 39 92 4b 41   ....<..r.R[.9.KA
00000010: b9 a8 f8 9a e9 53 0d a3   18 84 2e c0 8a 80 b5 aa   .....S..........
00000020: af b5 cf b5 25 2a 36 c3   52 ff b1 f2 b4 ae 13 33   ....%*6.R......3
00000030: bf 33 8d ed 17 9c 62 a0   91 80 fc 67 d0 8e 5d 6f   .3....b....g..]o
00000040: 8f af 85    

Versions

Version
0.1.0