From 8889daaa9be7af11db3e90982a2ef30a45585421 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Wed, 2 Sep 2009 23:08:35 +0000 Subject: [PATCH] tests for hex encoding git-svn-id: http://jclouds.googlecode.com/svn/trunk@1869 3d8758e0-26b5-11de-8745-db77d3ebf521 --- core/src/main/java/org/jclouds/http/HttpUtils.java | 2 ++ .../test/java/org/jclouds/util/HttpUtilsTest.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/core/src/main/java/org/jclouds/http/HttpUtils.java b/core/src/main/java/org/jclouds/http/HttpUtils.java index 914e32a1cf..37b42ff718 100644 --- a/core/src/main/java/org/jclouds/http/HttpUtils.java +++ b/core/src/main/java/org/jclouds/http/HttpUtils.java @@ -69,6 +69,8 @@ public class HttpUtils { } public static byte[] fromHexString(String hex) { + if (hex.startsWith("0x")) + hex = hex.substring(2); byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); diff --git a/core/src/test/java/org/jclouds/util/HttpUtilsTest.java b/core/src/test/java/org/jclouds/util/HttpUtilsTest.java index 1b1e302970..01583fc069 100644 --- a/core/src/test/java/org/jclouds/util/HttpUtilsTest.java +++ b/core/src/test/java/org/jclouds/util/HttpUtilsTest.java @@ -121,4 +121,18 @@ public class HttpUtilsTest extends PerformanceTest { assertEquals(base64Digest, b64); } + byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 }; + String hex = "0001020408102040"; + + public void testHexStringEncode() throws UnsupportedEncodingException { + assertEquals(HttpUtils.toHexString(bytes), hex); + } + + public void testHexStringDecode() throws UnsupportedEncodingException { + assertEquals(HttpUtils.fromHexString(hex), bytes); + } + + public void testHexStringDecodeOx() throws UnsupportedEncodingException { + assertEquals(HttpUtils.fromHexString("0x" + hex), bytes); + } }