HexUtils: fix empty string handling (#1907)

Fixes #1903

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
This commit is contained in:
Markus Rathgeb
2020-12-09 09:57:39 +01:00
committed by GitHub
parent ff4442b021
commit 406f83cbb6
2 changed files with 13 additions and 1 deletions
@@ -92,6 +92,10 @@ public class HexUtils {
* @return the corresponding byte array
*/
public static byte[] hexToBytes(String hexString, String delimiter) {
if (hexString.isEmpty()) {
return new byte[0];
}
// first convert to upper case to ease the rest
String ucHexString = hexString.toUpperCase();
@@ -12,7 +12,7 @@
*/
package org.openhab.core.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
@@ -62,4 +62,12 @@ public class HexUtilsTest {
byte[] result = HexUtils.hexToBytes("41-:-42-:-43-:-44", "-:-");
assertEquals("ABCD", new String(result));
}
@Test
public void testEmptyByteArray() {
final byte[] input = new byte[0];
final String str = HexUtils.bytesToHex(input);
final byte[] output = HexUtils.hexToBytes(str);
assertArrayEquals(input, output);
}
}