Last active
August 29, 2015 14:21
-
-
Save jesperdj/c193289a367f98aecf05 to your computer and use it in GitHub Desktop.
Utility class for parsing and formatting hex strings to / from byte arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2015 Jesper de Jong | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.jesperdj.sandbox; | |
import java.io.ByteArrayOutputStream; | |
public final class HexUtil { | |
private HexUtil() { | |
} | |
public static byte[] parseHex(String hex) { | |
final int len = hex.length(); | |
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(len / 2); | |
for (int i = 0; i < len; i += 2) { | |
buffer.write((charToHexDigit(hex.charAt(i)) << 4) | charToHexDigit(hex.charAt(i + 1))); | |
} | |
return buffer.toByteArray(); | |
} | |
public static String formatHex(byte[] bytes) { | |
final StringBuilder sb = new StringBuilder(bytes.length * 2); | |
for (byte b : bytes) { | |
sb.append(hexDigitToChar((b >> 4) & 0xF)).append(hexDigitToChar(b & 0xF)); | |
} | |
return sb.toString(); | |
} | |
private static int charToHexDigit(char c) { | |
return c - (c >= 'a' ? 'W' : c >= 'A' ? '7' : '0'); | |
} | |
private static char hexDigitToChar(int d) { | |
return (char) (d + (d >= 10 ? '7' : '0')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2015 Jesper de Jong | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.jesperdj.sandbox; | |
import org.junit.Test; | |
import static com.jesperdj.sandbox.HexUtil.formatHex; | |
import static com.jesperdj.sandbox.HexUtil.parseHex; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
public class HexUtilTest { | |
@Test | |
public void testParseHexZeroLength() { | |
assertThat(parseHex(""), is(new byte[0])); | |
} | |
@Test | |
public void testParseHexSingleByte() { | |
for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { | |
assertThat(parseHex(String.format("%02X", b & 0xFF)), is(new byte[]{(byte) b})); | |
} | |
} | |
@Test | |
public void testParseHexMultipleBytes() { | |
assertThat(parseHex("0123456789ABCDEF"), | |
is(new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF})); | |
} | |
@Test | |
public void testFormatHexZeroLength() { | |
assertThat(formatHex(new byte[0]), is("")); | |
} | |
@Test | |
public void testFormatHexSingleByte() { | |
for (int b = Byte.MIN_VALUE; b <= Byte.MAX_VALUE; b++) { | |
assertThat(formatHex(new byte[]{(byte) b}), is(String.format("%02X", b & 0xFF))); | |
} | |
} | |
@Test | |
public void testFormatHexMultipleBytes() { | |
assertThat(formatHex(new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF}), | |
is("0123456789ABCDEF")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment