From 68e178c43d8619b8b13aa78828c75e788d4d2621 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 7 Jan 2015 16:16:12 +1300 Subject: Add hex conversion method --- src/org/traccar/helper/ChannelBufferTools.java | 14 ++++++++++++++ test/org/traccar/helper/ChannelBufferToolsTest.java | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java index ef2ebc313..0de07610d 100644 --- a/src/org/traccar/helper/ChannelBufferTools.java +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -17,6 +17,7 @@ package org.traccar.helper; import java.util.Formatter; import org.jboss.netty.buffer.ChannelBuffer; +import org.traccar.protocol.IntellitracFrameDecoder; /** * ChannelBuffer helper methods @@ -130,4 +131,17 @@ public class ChannelBufferTools { } return out; } + + /** + * Convert hex string to byte array + */ + public static byte[] convertHexString(String in) { + int count = in.length() / 2; + byte[] out = new byte[count]; + for (int i = 0; i < count; i++) { + out[i] = Integer.valueOf(in.substring(i * 2, (i + 1) * 2), 16).byteValue(); + } + return out; + } + } diff --git a/test/org/traccar/helper/ChannelBufferToolsTest.java b/test/org/traccar/helper/ChannelBufferToolsTest.java index 792770dab..67c58058b 100644 --- a/test/org/traccar/helper/ChannelBufferToolsTest.java +++ b/test/org/traccar/helper/ChannelBufferToolsTest.java @@ -31,4 +31,10 @@ public class ChannelBufferToolsTest { result = Long.valueOf(result).toString(); assertEquals("1903", result); } + + @Test + public void convertHexStringTest() { + assertArrayEquals(ChannelBufferTools.convertHexString("1234"), new byte[] {0x12,0x34}); + } + } -- cgit v1.2.3