forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL - 2420 Converting Float to Byte Array and vice-versa in Java (eu…
…genp#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
- Loading branch information
1 parent
7fa2aba
commit effde80
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
core-java-arrays/src/main/java/com/baeldung/array/conversions/FloatToByteArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
core-java-arrays/src/test/java/com/baeldung/array/conversions/FloatToByteArrayUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |