diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-01 11:04:03 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-01 11:04:03 +1200 |
commit | c099628f0cd07ca6545773a69824b567a6610675 (patch) | |
tree | 52319ee2fc4667cf1566f3b4bed21b9780b8b005 | |
parent | d6662317306b79717f9d3a1df30fc83cb87866ce (diff) | |
download | trackermap-server-c099628f0cd07ca6545773a69824b567a6610675.tar.gz trackermap-server-c099628f0cd07ca6545773a69824b567a6610675.tar.bz2 trackermap-server-c099628f0cd07ca6545773a69824b567a6610675.zip |
Add byte array conversion method
-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})); } } |