tests for hex encoding

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1869 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-02 23:08:35 +00:00
parent 3a569021dc
commit 8889daaa9b
2 changed files with 16 additions and 0 deletions

View File

@ -69,6 +69,8 @@ public class HttpUtils {
} }
public static byte[] fromHexString(String hex) { public static byte[] fromHexString(String hex) {
if (hex.startsWith("0x"))
hex = hex.substring(2);
byte[] bytes = new byte[hex.length() / 2]; byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) { for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);

View File

@ -121,4 +121,18 @@ public class HttpUtilsTest extends PerformanceTest {
assertEquals(base64Digest, b64); 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);
}
} }