Skip to content

Commit

Permalink
Issue #27 implemented, bugfix in class Conversion, added a few more u…
Browse files Browse the repository at this point in the history
…nit tests.
  • Loading branch information
marco-wehnert authored and doip committed Feb 7, 2020
1 parent 37d4bac commit 41476df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
46 changes: 19 additions & 27 deletions src/main/java/doip/library/util/Conversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,35 @@ public static byte[] hexStringToByteArray(String s) {
* @return The hex string
*/
public static String byteArrayToHexString(byte[] bytes) {
if (bytes.length == 0) return "";
char[] hexChars = new char[bytes.length * 3 - 1];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 3] = hexArray[v >>> 4];
hexChars[j * 3 + 1] = hexArray[v & 0x0F];
if (j < bytes.length - 1) {
hexChars[j * 3 + 2] = ' ';
}
}
return new String(hexChars);
return byteArrayToHexStringShort(bytes, bytes.length);
}

/**
* Converts a byte array to a hex string but it will
* take only 'count' bytes.
* take only 'count' bytes. If the byte array is smaller than
* 'count' the byte array will be converted as it is.
* @param bytes The byte array which shall be converted
* @param count Then maximum number of bytes which will be converted
* @param count The maximum number of bytes which shall be converted
* @return The hex string
*/
public static String byteArrayToHexStringShort(byte[] bytes, int count) {
if (bytes.length == 0) {
return "";
} else if (bytes.length <= count) {
return byteArrayToHexString(bytes);
} else {
char[] hexChars = new char[count * 3 - 1];
for (int j = 0; j < count; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 3] = hexArray[v >>> 4];
hexChars[j * 3 + 1] = hexArray[v & 0x0F];
if (j < count - 1) {
hexChars[j * 3 + 2] = ' ';
}
if (bytes.length == 0) return "";
if (count == 0) return "";
if (bytes.length < count) count = bytes.length;

// Don't call byteArrayToHexString because that will require
// to create a new byte array with reduced size and this can be
// time consuming
char[] hexChars = new char[count * 3 - 1];
for (int j = 0; j < count; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 3] = hexArray[v >>> 4];
hexChars[j * 3 + 1] = hexArray[v & 0x0F];
if (j < count - 1) {
hexChars[j * 3 + 2] = ' ';
}
return new String(hexChars);
}
return new String(hexChars);
}

public static String byteArrayToHexStringShortDotted(byte[] bytes, int count) {
Expand Down
32 changes: 21 additions & 11 deletions src/test/java/doip/library/util/TestConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,50 @@

import static doip.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import doip.library.util.Conversion;

public class TestConversion {

@Test
public void testA() {
public void testHexStringoByteArray() {
assertArrayEquals(new byte[0], Conversion.hexStringToByteArray(""));
assertArrayEquals(new byte[] {0x00, (byte)0xA0}, Conversion.hexStringToByteArray("00 A0"));
assertArrayEquals(new byte[] {0x00, (byte)0xA0}, Conversion.hexStringToByteArray("00 A0..;;,,"));
}

@Test
public void testB() {
assertArrayEquals(new byte[0], Conversion.hexStringToByteArray(""));
}

@Test
public void testC() {
public void testByteArrayToHexString() {
byte[] bytes = new byte[] {0x22, (byte) 0xF1, (byte) 0x86};
assertEquals("22 F1 86", Conversion.byteArrayToHexString(bytes));
bytes = new byte[0];
assertEquals("", Conversion.byteArrayToHexString(bytes));
}

@Test
public void testD() {
public void testByteArrayToHexStringShort() {
byte[] bytes = new byte[] {1,2,3,4,5,6,7,8};
assertEquals("01 02 03 04", Conversion.byteArrayToHexStringShort(bytes, 4));
assertEquals("01 02 03 04...", Conversion.byteArrayToHexStringShortDotted(bytes, 4));
assertEquals("01 02 03 04 05 06 07 08", Conversion.byteArrayToHexStringShort(bytes, 10));
}

@Test
public void testE() {
public void testByteArrayToHexStringShortDotted() {
byte[] bytes = new byte[0];
assertEquals("", Conversion.byteArrayToHexString(bytes));
assertEquals("", Conversion.byteArrayToHexStringShortDotted(bytes, 0));
bytes = new byte[] {1,2,3};
assertEquals("...", Conversion.byteArrayToHexStringShortDotted(bytes, 0));
bytes = new byte[] {1,2,3,4,5,6,7,8};
assertEquals("01 02 03 04...", Conversion.byteArrayToHexStringShortDotted(bytes, 4));
}

@Test
public void testAsciiStringToByteArray() {
byte[] actuals = Conversion.asciiStringToByteArray("ABC 123");
byte[] expecteds = new byte[] {0x41, 0x42, 0x43, 0x20, 0x31, 0x32, 0x33};
Assert.assertArrayEquals(expecteds, actuals);
}
}

0 comments on commit 41476df

Please sign in to comment.