FEAT StringUtils.toHex(byte[]) method

master
Niclas Thobaben 2020-12-04 15:16:28 +01:00
parent f4e2cd940c
commit 5d0f99dc0b
1 changed files with 19 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package de.nclazz.commons;
import lombok.NonNull;
public abstract class StringUtils {
private StringUtils() { /* no-op */ }
@ -75,4 +77,21 @@ public abstract class StringUtils {
return sb.toString();
}
/**
* Create a hex-{@code String} from given byte array,
* @param bytes array
* @return new {@code String}
* @throws NullPointerException if bytes are {@code null}
*/
public static String toHex(@NonNull byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length*2);
for (int v : bytes) {
sb.append(Character.forDigit((v >> 4) & 0xF, 16))
.append(Character.forDigit((v & 0xF), 16));
}
return sb.toString();
}
}