BAEL-4522: Convert Byte Array to its Numeric Representation (#11267)

* Convert Byte Array to its Numeric Representation

* Remove Redundant Getter Method
This commit is contained in:
lsieun 2021-10-17 13:29:38 +08:00 committed by GitHub
parent 7dfea6394e
commit 6d1cdbe9d7
3 changed files with 602 additions and 0 deletions

View File

@ -19,6 +19,11 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,281 @@
package com.baeldung.array.conversions;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Conversion;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Arrays;
class ByteArrayToNumericRepresentation {
static int convertByteArrayToIntUsingShiftOperator(byte[] bytes) {
int value = 0;
for (byte b : bytes) {
value = (value << 8) + (b & 0xFF);
}
return value;
}
static byte[] convertIntToByteArrayUsingShiftOperator(int value) {
byte[] bytes = new byte[Integer.BYTES];
int length = bytes.length;
for (int i = 0; i < length; i++) {
bytes[length - i - 1] = (byte) (value & 0xFF);
value >>= 8;
}
return bytes;
}
static long convertByteArrayToLongUsingShiftOperator(byte[] bytes) {
long value = 0;
for (byte b : bytes) {
value <<= 8;
value |= (b & 0xFF);
}
return value;
}
static byte[] convertLongToByteArrayUsingShiftOperator(long value) {
byte[] bytes = new byte[Long.BYTES];
int length = bytes.length;
for (int i = 0; i < length; i++) {
bytes[length - i - 1] = (byte) (value & 0xFF);
value >>= 8;
}
return bytes;
}
static float convertByteArrayToFloatUsingShiftOperator(byte[] bytes) {
// convert bytes to int
int intValue = 0;
for (byte b : bytes) {
intValue = (intValue << 8) + (b & 0xFF);
}
// convert int to float
return Float.intBitsToFloat(intValue);
}
static byte[] convertFloatToByteArrayUsingShiftOperator(float value) {
// convert float to int
int intValue = Float.floatToIntBits(value);
// convert int to bytes
byte[] bytes = new byte[Float.BYTES];
int length = bytes.length;
for (int i = 0; i < length; i++) {
bytes[length - i - 1] = (byte) (intValue & 0xFF);
intValue >>= 8;
}
return bytes;
}
static double convertingByteArrayToDoubleUsingShiftOperator(byte[] bytes) {
long longValue = 0;
for (byte b : bytes) {
longValue = (longValue << 8) + (b & 0xFF);
}
return Double.longBitsToDouble(longValue);
}
static byte[] convertDoubleToByteArrayUsingShiftOperator(double value) {
long longValue = Double.doubleToLongBits(value);
byte[] bytes = new byte[Double.BYTES];
int length = bytes.length;
for (int i = 0; i < length; i++) {
bytes[length - i - 1] = (byte) (longValue & 0xFF);
longValue >>= 8;
}
return bytes;
}
static int convertByteArrayToIntUsingByteBuffer(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.put(bytes);
buffer.rewind();
return buffer.getInt();
}
static byte[] convertIntToByteArrayUsingByteBuffer(int value) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.putInt(value);
buffer.rewind();
return buffer.array();
}
static long convertByteArrayToLongUsingByteBuffer(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.rewind();
return buffer.getLong();
}
static byte[] convertLongToByteArrayUsingByteBuffer(long value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(value);
buffer.rewind();
return buffer.array();
}
static float convertByteArrayToFloatUsingByteBuffer(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
buffer.put(bytes);
buffer.rewind();
return buffer.getFloat();
}
static byte[] convertFloatToByteArrayUsingByteBuffer(float value) {
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
buffer.putFloat(value);
buffer.rewind();
return buffer.array();
}
static double convertByteArrayToDoubleUsingByteBuffer(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
buffer.put(bytes);
buffer.rewind();
return buffer.getDouble();
}
static byte[] convertDoubleToByteArrayUsingByteBuffer(double value) {
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
buffer.putDouble(value);
buffer.rewind();
return buffer.array();
}
static int convertByteArrayToIntUsingBigInteger(byte[] bytes) {
return new BigInteger(bytes).intValue();
}
static byte[] convertIntToByteArrayUsingBigInteger(int value) {
return BigInteger.valueOf(value).toByteArray();
}
static long convertByteArrayToLongUsingBigInteger(byte[] bytes) {
return new BigInteger(bytes).longValue();
}
static byte[] convertLongToByteArrayUsingBigInteger(long value) {
return BigInteger.valueOf(value).toByteArray();
}
static float convertByteArrayToFloatUsingBigInteger(byte[] bytes) {
int intValue = new BigInteger(bytes).intValue();
return Float.intBitsToFloat(intValue);
}
static byte[] convertFloatToByteArrayUsingBigInteger(float value) {
int intValue = Float.floatToIntBits(value);
return BigInteger.valueOf(intValue).toByteArray();
}
static double convertByteArrayToDoubleUsingBigInteger(byte[] bytes) {
long longValue = new BigInteger(bytes).longValue();
return Double.longBitsToDouble(longValue);
}
static byte[] convertDoubleToByteArrayUsingBigInteger(double value) {
long longValue = Double.doubleToLongBits(value);
return BigInteger.valueOf(longValue).toByteArray();
}
static int convertingByteArrayToIntUsingGuava(byte[] bytes) {
return Ints.fromByteArray(bytes);
}
static byte[] convertIntToByteArrayUsingGuava(int value) {
return Ints.toByteArray(value);
}
static long convertByteArrayToLongUsingGuava(byte[] bytes) {
return Longs.fromByteArray(bytes);
}
static byte[] convertLongToByteArrayUsingGuava(long value) {
return Longs.toByteArray(value);
}
static float convertByteArrayToFloatUsingGuava(byte[] bytes) {
int intValue = Ints.fromByteArray(bytes);
return Float.intBitsToFloat(intValue);
}
static byte[] convertFloatToByteArrayUsingGuava(float value) {
int intValue = Float.floatToIntBits(value);
return Ints.toByteArray(intValue);
}
static double convertByteArrayToDoubleUsingGuava(byte[] bytes) {
long longValue = Longs.fromByteArray(bytes);
return Double.longBitsToDouble(longValue);
}
static byte[] convertDoubleToByteArrayUsingGuava(double value) {
long longValue = Double.doubleToLongBits(value);
return Longs.toByteArray(longValue);
}
static int convertByteArrayToIntUsingCommonsLang(byte[] bytes) {
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
ArrayUtils.reverse(copyBytes);
return Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
}
static byte[] convertIntToByteArrayUsingCommonsLang(int value) {
byte[] bytes = new byte[Integer.BYTES];
Conversion.intToByteArray(value, 0, bytes, 0, bytes.length);
ArrayUtils.reverse(bytes);
return bytes;
}
static long convertByteArrayToLongUsingCommonsLang(byte[] bytes) {
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
ArrayUtils.reverse(copyBytes);
return Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
}
static byte[] convertLongToByteArrayUsingCommonsLang(long value) {
byte[] bytes = new byte[Long.BYTES];
Conversion.longToByteArray(value, 0, bytes, 0, bytes.length);
ArrayUtils.reverse(bytes);
return bytes;
}
static float convertByteArrayToFloatUsingCommonsLang(byte[] bytes) {
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
ArrayUtils.reverse(copyBytes);
int intValue = Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
return Float.intBitsToFloat(intValue);
}
static byte[] convertFloatToByteArrayUsingCommonsLang(float value) {
int intValue = Float.floatToIntBits(value);
byte[] bytes = new byte[Float.BYTES];
Conversion.intToByteArray(intValue, 0, bytes, 0, bytes.length);
ArrayUtils.reverse(bytes);
return bytes;
}
static double convertByteArrayToDoubleUsingCommonsLang(byte[] bytes) {
byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
ArrayUtils.reverse(copyBytes);
long longValue = Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
return Double.longBitsToDouble(longValue);
}
static byte[] convertDoubleToByteArrayUsingCommonsLang(double value) {
long longValue = Double.doubleToLongBits(value);
byte[] bytes = new byte[Long.BYTES];
Conversion.longToByteArray(longValue, 0, bytes, 0, bytes.length);
ArrayUtils.reverse(bytes);
return bytes;
}
}

View File

@ -0,0 +1,316 @@
package com.baeldung.array.conversions;
import org.junit.Test;
import static com.baeldung.array.conversions.ByteArrayToNumericRepresentation.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class ByteArrayToNumericRepresentationUnitTest {
private static final byte[] INT_BYTE_ARRAY = new byte[]{
(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE
};
private static final int INT_VALUE = 0xCAFEBABE;
private static final byte[] FLOAT_BYTE_ARRAY = new byte[]{
(byte) 0x40, (byte) 0x48, (byte) 0xF5, (byte) 0xC3
};
private static final float FLOAT_VALUE = 3.14F;
private static final byte[] LONG_BYTE_ARRAY = new byte[]{
(byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
(byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF
};
private static final long LONG_VALUE = 0x0123456789ABCDEFL;
private static final byte[] DOUBLE_BYTE_ARRAY = new byte[]{
(byte) 0x3F, (byte) 0xE3, (byte) 0xC6, (byte) 0xA7,
(byte) 0xEF, (byte) 0x9D, (byte) 0xB2, (byte) 0x2D
};
private static final double DOUBLE_VALUE = 0.618D;
@Test
public void givenShiftOperator_whenConvertingByteArrayToInt_thenSuccess() {
int value = convertByteArrayToIntUsingShiftOperator(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenShiftOperator_whenConvertingIntToByteArray_thenSuccess() {
byte[] bytes = convertIntToByteArrayUsingShiftOperator(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToLong_thenSuccess() {
long value = convertByteArrayToLongUsingShiftOperator(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenShiftOperator_whenConvertingLongToByteArray_thenSuccess() {
byte[] bytes = convertLongToByteArrayUsingShiftOperator(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToFloat_thenSuccess() {
float value = convertByteArrayToFloatUsingShiftOperator(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenShiftOperator_whenConvertingFloatToByteArray_thenSuccess() {
byte[] bytes = convertFloatToByteArrayUsingShiftOperator(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToDouble_thenSuccess() {
double value = convertingByteArrayToDoubleUsingShiftOperator(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenShiftOperator_whenConvertingDoubleToByteArray_thenSuccess() {
byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToInt_thenSuccess() {
int value = convertByteArrayToIntUsingByteBuffer(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenByteBuffer_whenConvertingIntToByteArray_thenSuccess() {
byte[] bytes = convertIntToByteArrayUsingByteBuffer(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToLong_thenSuccess() {
long value = convertByteArrayToLongUsingByteBuffer(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenByteBuffer_whenConvertingLongToByteArray_thenSuccess() {
byte[] bytes = convertLongToByteArrayUsingByteBuffer(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToFloat_thenSuccess() {
float value = convertByteArrayToFloatUsingByteBuffer(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenByteBuffer_whenConvertingFloatToByteArray_thenSuccess() {
byte[] bytes = convertFloatToByteArrayUsingByteBuffer(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToDouble_thenSuccess() {
double value = convertByteArrayToDoubleUsingByteBuffer(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenByteBuffer_whenConvertingDoubleToByteArray_thenSuccess() {
byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToInt_thenSuccess() {
int value = convertByteArrayToIntUsingBigInteger(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenBigInteger_whenConvertingIntToByteArray_thenSuccess() {
byte[] bytes = convertIntToByteArrayUsingBigInteger(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToLong_thenSuccess() {
long value = convertByteArrayToLongUsingBigInteger(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenBigInteger_whenConvertingLongToByteArray_thenSuccess() {
byte[] bytes = convertLongToByteArrayUsingBigInteger(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToFloat_thenSuccess() {
float value = convertByteArrayToFloatUsingBigInteger(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenBigInteger_whenConvertingFloatToByteArray_thenSuccess() {
byte[] bytes = convertFloatToByteArrayUsingBigInteger(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToDouble_thenSuccess() {
double value = convertByteArrayToDoubleUsingBigInteger(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenBigInteger_whenConvertingDoubleToByteArray_thenSuccess() {
byte[] bytes = convertDoubleToByteArrayUsingBigInteger(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToInt_thenSuccess() {
int value = convertingByteArrayToIntUsingGuava(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenGuava_whenConvertingIntToByteArray_thenSuccess() {
byte[] bytes = convertIntToByteArrayUsingGuava(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToLong_thenSuccess() {
long value = convertByteArrayToLongUsingGuava(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenGuava_whenConvertingLongToByteArray_thenSuccess() {
byte[] bytes = convertLongToByteArrayUsingGuava(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToFloat_thenSuccess() {
float value = convertByteArrayToFloatUsingGuava(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenGuava_whenConvertingFloatToByteArray_thenSuccess() {
byte[] bytes = convertFloatToByteArrayUsingGuava(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToDouble_thenSuccess() {
double value = convertByteArrayToDoubleUsingGuava(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenGuava_whenConvertingDoubleToByteArray_thenSuccess() {
byte[] bytes = convertDoubleToByteArrayUsingGuava(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToInt_thenSuccess() {
int value = convertByteArrayToIntUsingCommonsLang(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenCommonsLang_whenConvertingIntToByteArray_thenSuccess() {
byte[] bytes = convertIntToByteArrayUsingCommonsLang(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToLong_thenSuccess() {
long value = convertByteArrayToLongUsingCommonsLang(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenCommonsLang_whenConvertingLongToByteArray_thenSuccess() {
byte[] bytes = convertLongToByteArrayUsingCommonsLang(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToFloat_thenSuccess() {
float value = convertByteArrayToFloatUsingCommonsLang(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenCommonsLang_whenConvertingFloatToByteArray_thenSuccess() {
byte[] bytes = convertFloatToByteArrayUsingCommonsLang(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToDouble_thenSuccess() {
double value = convertByteArrayToDoubleUsingCommonsLang(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenCommonsLang_whenConvertingDoubleToByteArray_thenSuccess() {
byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
}