diff options
-rw-r--r-- | src/org/traccar/helper/ChannelBufferTools.java | 13 | ||||
-rw-r--r-- | test/org/traccar/helper/ChannelBufferToolsTest.java | 7 |
2 files changed, 19 insertions, 1 deletions
diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java index 09e3d5191..ef9384138 100644 --- a/src/org/traccar/helper/ChannelBufferTools.java +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -15,6 +15,7 @@ */ package org.traccar.helper; +import java.math.BigInteger; import java.util.Formatter; import org.jboss.netty.buffer.ChannelBuffer; import org.traccar.protocol.IntellitracFrameDecoder; @@ -144,4 +145,16 @@ public class ChannelBufferTools { return out; } + /** + * Convert byte array to hex string + */ + public static String convertByteArray(byte[] in) { + StringBuilder out = new StringBuilder(); + Formatter formatter = new Formatter(out); + for (byte b : in) { + formatter.format("%02x", b); + } + return out.toString(); + } + } diff --git a/test/org/traccar/helper/ChannelBufferToolsTest.java b/test/org/traccar/helper/ChannelBufferToolsTest.java index 67c58058b..90f5e82ae 100644 --- a/test/org/traccar/helper/ChannelBufferToolsTest.java +++ b/test/org/traccar/helper/ChannelBufferToolsTest.java @@ -34,7 +34,12 @@ public class ChannelBufferToolsTest { @Test public void convertHexStringTest() { - assertArrayEquals(ChannelBufferTools.convertHexString("1234"), new byte[] {0x12,0x34}); + assertArrayEquals(new byte[] {0x12,0x34}, ChannelBufferTools.convertHexString("1234")); + } + + @Test + public void convertHexByteArray() { + assertEquals("1234", ChannelBufferTools.convertByteArray(new byte[] {0x12,0x34})); } } |