summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/isode/stroke/stringcodecs/Hexify.java24
-rw-r--r--test/com/isode/stroke/stringcodecs/HexifyTest.java43
2 files changed, 65 insertions, 2 deletions
diff --git a/src/com/isode/stroke/stringcodecs/Hexify.java b/src/com/isode/stroke/stringcodecs/Hexify.java
index 8dda1c2..52facb4 100644
--- a/src/com/isode/stroke/stringcodecs/Hexify.java
+++ b/src/com/isode/stroke/stringcodecs/Hexify.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 Isode Limited, London, England.
+ * Copyright (c) 2011-2012 Isode Limited, London, England.
* All rights reserved.
*/
/*
@@ -13,7 +13,7 @@ import com.isode.stroke.base.ByteArray;
public class Hexify {
public static String hexify(byte datum) {
- return String.format("%x", new Byte(datum));
+ return String.format("%02x", new Byte(datum));
}
public static String hexify(ByteArray data) {
@@ -23,4 +23,24 @@ public class Hexify {
}
return result.toString();
}
+
+
+
+ public static ByteArray unhexify(String hexstring) {
+ if (hexstring.length() % 2 != 0) {
+ return new ByteArray();
+ }
+ byte[] result = new byte[hexstring.length() / 2];
+ for (int pos = 0; pos < hexstring.length() - 1; pos += 2) {
+ char c;
+ c = hexstring.charAt(pos);
+ int a = (c>='0'&&c<='9') ? c-'0' : (c>='A'&&c<='Z') ? c-'A' + 10 : (c>='a'&&c<='z') ? c-'a' + 10 : -1;
+ c = hexstring.charAt(pos+1);
+ int b = (c>='0'&&c<='9') ? c-'0' : (c>='A'&&c<='Z') ? c-'A' + 10 : (c>='a'&&c<='z') ? c-'a' + 10 : -1;
+ if (a == -1 || b == -1) return new ByteArray(); // fail
+ result[pos/2] = (byte) ((a << 4) | b);
+
+ }
+ return new ByteArray(result);
+ }
}
diff --git a/test/com/isode/stroke/stringcodecs/HexifyTest.java b/test/com/isode/stroke/stringcodecs/HexifyTest.java
new file mode 100644
index 0000000..4d6dc0d
--- /dev/null
+++ b/test/com/isode/stroke/stringcodecs/HexifyTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012, Isode Limited, London, England.
+ * All rights reserved.
+ */
+/*
+ * Copyright (c) 2010, Remko Tronçon.
+ * All rights reserved.
+ */
+package com.isode.stroke.stringcodecs;
+
+import com.isode.stroke.base.ByteArray;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class HexifyTest {
+
+ private ByteArray fix(int[] vals) {
+ byte[] fixed = new byte[vals.length];
+ for (int i = 0; i < fixed.length; i++) {
+ fixed[i] = (byte) vals[i];
+ }
+ return new ByteArray(fixed);
+ }
+
+ @Test
+ public void testHexify() {
+ assertEquals("4206b23ca6b0a643d20d89b04ff58cf78b8096ed", Hexify.hexify(fix(new int[]{0x42, 0x06, 0xb2, 0x3c, 0xa6, 0xb0, 0xa6, 0x43, 0xd2, 0x0d, 0x89, 0xb0, 0x4f, 0xf5, 0x8c, 0xf7, 0x8b, 0x80, 0x96, 0xed})));
+ }
+
+ @Test
+ public void testHexify_Byte() {
+ assertEquals("b2", Hexify.hexify((byte) 0xb2));
+ }
+
+ @Test
+ public void testUnhexify() {
+ assertEquals("ffaf02", Hexify.hexify(Hexify.unhexify("ffaf02")));
+ assertEquals(new ByteArray(fix(new int[]{0x01, 0x23, 0xf2})), Hexify.unhexify("0123f2"));
+ }
+};
+