A byte-array conversion needs an agreed width and byte order. Reading the correct number of bytes with the wrong convention can produce a valid-looking value that means something else.
The original NByte helper grouped conversions for short, int, long, float, and double, including offset-based reads and array slicing.
Read the contract before the helper
Check the number of available bytes, the starting offset, the protocol’s byte order, and the intended slice endpoints. The source mixes explicit bit operations with ByteBuffer use, so review the chosen method rather than assuming every conversion has an identical implementation.
Read the historical Java helper
package com.nhancv.chart.utils;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Original example, 2016-09-28.
*/
public class NByte {
private static final short INT_BYTE = Integer.SIZE / Byte.SIZE;
private static final short SHORT_BYTE = Short.SIZE / Byte.SIZE;
private static final short LONG_BYTE = Long.SIZE / Byte.SIZE;
private static final short FLOAT_BYTE = Float.SIZE / Byte.SIZE;
private static final short DOUBLE_BYTE = Double.SIZE / Byte.SIZE;
/**
* Convert long to byte array
*
* @param x
* @return
*/
public static byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(LONG_BYTE);
buffer.putLong(0, x);
return buffer.array();
}
/**
* Convert byte array to long
*
* @param bytes
* @return
*/
public static long bytesToLong(byte[] bytes) {
return bytesToLong(bytes, 0);
}
/**
* Convert byte array to long with offset
*
* @param bytes
* @param offset
* @return
*/
public static long bytesToLong(byte[] bytes, int offset) {
ByteBuffer buffer = ByteBuffer.allocate(LONG_BYTE);
buffer.put(bytes, offset, LONG_BYTE);
buffer.flip();
return buffer.getLong();
}
/**
* Convert int to byte array
*
* @param x
* @return
*/
public static byte[] intToBytes(int x) {
ByteBuffer buffer = ByteBuffer.allocate(INT_BYTE);
buffer.putInt(0, x);
return buffer.array();
}
/**
* Convert byte array to int
*
* @param bytes
* @return
*/
public static int bytesToInt(byte[] bytes) {
return bytesToInt(bytes, 0);
}
/**
* Convert byte array to int with offset
*
* @param bytes
* @param offset
* @return
*/
public static int bytesToInt(byte[] bytes, int offset) {
ByteBuffer buffer = ByteBuffer.allocate(INT_BYTE);
buffer.put(bytes, offset, INT_BYTE);
buffer.flip();
return buffer.getInt();
}
/**
* Convert short to byte array
*
* @param x
* @return
*/
public static byte[] shortToBytes(short x) {
ByteBuffer buffer = ByteBuffer.allocate(SHORT_BYTE);
buffer.putInt(0, x);
return buffer.array();
}
/**
* Convert byte array to short
*
* @param bytes
* @return
*/
public static short bytesToShort(byte[] bytes) {
return bytesToShort(bytes, 0);
}
/**
* Convert byte array to short with offset
*
* @param bytes
* @param offset
* @return
*/
public static short bytesToShort(byte[] bytes, int offset) {
ByteBuffer buffer = ByteBuffer.allocate(SHORT_BYTE);
buffer.put(bytes, offset, SHORT_BYTE);
buffer.flip();
return buffer.getShort();
}
/**
* Convert float to byte array
*
* @param x
* @return
*/
public static byte[] floatToBytes(float x) {
ByteBuffer buffer = ByteBuffer.allocate(FLOAT_BYTE);
buffer.putFloat(0, x);
return buffer.array();
}
/**
* Convert byte array to float
*
* @param bytes
* @return
*/
public static float bytesToFloat(byte[] bytes) {
return bytesToFloat(bytes, 0);
}
/**
* Convert byte array to float with offset
*
* @param bytes
* @return
*/
public static float bytesToFloat(byte[] bytes, int offset) {
ByteBuffer buffer = ByteBuffer.allocate(FLOAT_BYTE);
buffer.put(bytes, offset, FLOAT_BYTE);
buffer.flip();
return buffer.getFloat();
}
/**
* Convert double to byte array
*
* @param x
* @return
*/
public static byte[] doubleToBytes(double x) {
ByteBuffer buffer = ByteBuffer.allocate(DOUBLE_BYTE);
buffer.putDouble(0, x);
return buffer.array();
}
/**
* Convert byte array to double
*
* @param bytes
* @return
*/
public static double bytesToDouble(byte[] bytes) {
return bytesToDouble(bytes, 0);
}
/**
* Convert byte array to double with offset
*
* @param bytes
* @return
*/
public static double bytesToDouble(byte[] bytes, int offset) {
ByteBuffer buffer = ByteBuffer.allocate(DOUBLE_BYTE);
buffer.put(bytes, offset, DOUBLE_BYTE);
buffer.flip();
return buffer.getDouble();
}
/**
* Make new byte array after remove first n byte in original byte array. Original array will not change
*
* @param bytes
* @param n
* @return
*/
public static byte[] removeFirstNByte(byte[] bytes, int n) {
return Arrays.copyOfRange(bytes, n, bytes.length);
}
/**
* Make new byte array by cutting original array
*
* @param bytes
* @param startIndex
* @param endIndex
* @return
*/
public static byte[] subArray(byte[] bytes, int startIndex, int endIndex) {
return Arrays.copyOfRange(bytes, startIndex, endIndex);
}
}
The package name records the old application context. This listing has not been compiled or tested for this revision, and it is not a complete validation layer for untrusted binary input.