BAEL - 2420 Converting Float to Byte Array and vice-versa in Java (#6194)

* Hexagonal Architecture in Java - Spring boot app

* create README.md

* Update README.md

* Update README.md

* BAEL-2420 Converting Float to Byte Array and vice-versa in Java

* BAEL-2420 Converting Float to Byte Array and vice-versa in Java - conversions package added

* Hexagonal architecture code removed
This commit is contained in:
Anshul Bansal 2019-01-23 14:19:48 +02:00 committed by Eugen
parent 7fa2aba180
commit effde80333
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.array.conversions;
import java.nio.ByteBuffer;
public class FloatToByteArray {
/**
* convert float into byte array using Float API floatToIntBits
* @param value
* @return byte[]
*/
public static byte[] floatToByteArray(float value) {
int intBits = Float.floatToIntBits(value);
return new byte[] {(byte) (intBits >> 24), (byte) (intBits >> 16), (byte) (intBits >> 8), (byte) (intBits) };
}
/**
* convert byte array into float using Float API intBitsToFloat
* @param bytes
* @return float
*/
public static float byteArrayToFloat(byte[] bytes) {
int intBits = bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
return Float.intBitsToFloat(intBits);
}
/**
* convert float into byte array using ByteBuffer
* @param value
* @return byte[]
*/
public static byte[] floatToByteArrayWithByteBuffer(float value) {
return ByteBuffer.allocate(4).putFloat(value).array();
}
/**
* convert byte array into float using ByteBuffer
* @param bytes
* @return float
*/
public static float byteArrayToFloatWithByteBuffer(byte[] bytes) {
return ByteBuffer.wrap(bytes).getFloat();
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.array.conversions;
import static com.baeldung.array.conversions.FloatToByteArray.byteArrayToFloat;
import static com.baeldung.array.conversions.FloatToByteArray.byteArrayToFloatWithByteBuffer;
import static com.baeldung.array.conversions.FloatToByteArray.floatToByteArray;
import static com.baeldung.array.conversions.FloatToByteArray.floatToByteArrayWithByteBuffer;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class FloatToByteArrayUnitTest {
@Test
public void givenAFloat_thenConvertToByteArray() {
assertArrayEquals(new byte[] { 63, -116, -52, -51}, floatToByteArray(1.1f));
}
@Test
public void givenAByteArray_thenConvertToFloat() {
assertEquals(1.1f, byteArrayToFloat(new byte[] { 63, -116, -52, -51}), 0);
}
@Test
public void givenAFloat_thenConvertToByteArrayUsingByteBuffer() {
assertArrayEquals(new byte[] { 63, -116, -52, -51}, floatToByteArrayWithByteBuffer(1.1f));
}
@Test
public void givenAByteArray_thenConvertToFloatUsingByteBuffer() {
assertEquals(1.1f, byteArrayToFloatWithByteBuffer(new byte[] { 63, -116, -52, -51}), 0);
}
@Test
public void givenAFloat_thenConvertToByteArray_thenConvertToFloat() {
float floatToConvert = 200.12f;
byte[] byteArray = floatToByteArray(floatToConvert);
assertEquals(200.12f, byteArrayToFloat(byteArray), 0);
}
@Test
public void givenAFloat_thenConvertToByteArrayWithByteBuffer_thenConvertToFloatWithByteBuffer() {
float floatToConvert = 30100.42f;
byte[] byteArray = floatToByteArrayWithByteBuffer(floatToConvert);
assertEquals(30100.42f, byteArrayToFloatWithByteBuffer(byteArray), 0);
}
}