The list of methods to do Convert via ByteBuffer are organized into topic(s).
String
hexDump(byte... b) Returns the hex dump of the given byte array as 16 bytes per line
if (b == null)
return "";
StringBuffer buf = new StringBuffer();
int size = b.length;
for (int i = 0; i < size; i++) {
if ((i + 1) % 16 == 0) {
buf.append(zeropad(Integer.toHexString(byteToUInt(b[i])).toUpperCase(), 2));
buf.append('\n');
...
String
hexDump(byte[] buffer) hex Dump
StringBuilder buf = new StringBuilder(buffer.length << 1);
for (byte b : buffer)
addHexByte(buf, b);
return buf.toString();
ByteBuffer
hexStringToBytes(String hexString) hex String To Bytes
hexString = hexString.replaceAll("\\W+", "");
if (hexString.length() % 2 != 0) {
throw new RuntimeException("Input string's size must be even.");
byte[] result = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
result[i / 2] = (byte) Integer.valueOf(hexString.substring(i, i + 2), 16).intValue();
return ByteBuffer.wrap(result);
String
hexStrToStr(String hexStr) hex Str To Str
String decStr = "";
ByteBuffer bytes = ByteBuffer.allocate(hexStr.length() / 2);
for (int i = 0; i < hexStr.length(); i += 2) {
Byte b = (byte) (0xff & Integer.parseInt(hexStr.substring(i, i + 2), 16));
bytes.put(b);
bytes.position();
try {
...
byte[]
hexToAscii(byte[] src) hex To Ascii
byte[] asc = new byte[src.length * 2];
asc = rpBytes(src, src.length * 2, (byte) 0x00);
for (int i = 0; i < src.length; i++) {
asc[i * 2] = bcdToByte((byte) (src[i] >> 4 & 0x0f));
asc[i * 2 + 1] = bcdToByte((byte) (src[i] & 0x0f));
return asc;
byte[]
hexToBytes(String hexStr) hex To Bytes
ByteBuffer bytes = ByteBuffer.allocate(hexStr.length() / 2);
for (int i = 0; i < hexStr.length(); i += 2) {
Byte b = (byte) (0xff & Integer.parseInt(hexStr.substring(i, i + 2), 16));
bytes.put(b);
return bytes.array();
byte[]
hexToBytes(String hexString) hex To Bytes
byte[] bytes = new BigInteger(hexString, 16).toByteArray();
if (bytes.length > 0 && bytes[0] == 0) {
return Arrays.copyOfRange(bytes, 1, bytes.length);
} else {
return bytes;
List
intArrayFromBytes(byte[] bytes)
Unpack an array of big endian ints.
ImmutableList.Builder<Integer> list = ImmutableList.builder();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
while (buffer.hasRemaining()) {
list.add(buffer.getInt());
return list.build();
byte[]
intArrayToByteArray(int[] intArray) Convert from an array of ints to an array of bytes
byte[] byteArray = new byte[intArray.length * 4];
ByteBuffer byteBuf = ByteBuffer.wrap(byteArray);
IntBuffer intBuff = byteBuf.asIntBuffer();
intBuff.put(intArray);
return byteArray;
byte[]
intArrayToBytes(Collection values) Pack an array of ints as big endian.
ByteBuffer buffer = ByteBuffer.allocate(values.size() * Integer.BYTES);
for (int value : values) {
buffer.putInt(value);
return buffer.array();