FEAT StringUtils.toHex(byte[]) method
parent
f4e2cd940c
commit
5d0f99dc0b
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue