Skip to content

Commit 218c509

Browse files
committed
Add toFloat/Double
1 parent 1576867 commit 218c509

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## v0.4.3
4+
5+
* add toFloat/toDouble
6+
37
## v0.4.2
48

59
* add check method if transformer supports inplace

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>at.favre.lib</groupId>
88
<artifactId>bytes</artifactId>
9-
<version>0.4.2</version>
9+
<version>0.4.3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Bytes Utility Library</name>

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,37 @@ public long toLong() {
13191319
return resize(8).internalBuffer().getLong();
13201320
}
13211321

1322+
/**
1323+
* If the underlying byte array is smaller than or equal to 4 byte / 32 bit returns the
1324+
* representation for a Java float value. The output is dependent on the set {@link #byteOrder()}.
1325+
*
1326+
* @return the float representation
1327+
* @throws UnsupportedOperationException if byte array is longer than 4 byte
1328+
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
1329+
*/
1330+
public float toFloat() {
1331+
if (length() > 4) {
1332+
throw new UnsupportedOperationException("cannot convert to float if length > 4 byte");
1333+
}
1334+
return resize(4).internalBuffer().getFloat();
1335+
}
1336+
1337+
/**
1338+
* If the underlying byte array is smaller than or equal to 8 byte / 64 bit returns the
1339+
* representation for a Java float value. The output is dependent on the set {@link #byteOrder()}.
1340+
*
1341+
* @return the float representation
1342+
* @throws UnsupportedOperationException if byte array is longer than 8 byte
1343+
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
1344+
*/
1345+
public double toDouble() {
1346+
if (length() > 8) {
1347+
throw new UnsupportedOperationException("cannot convert to float if length > 8 byte");
1348+
}
1349+
return resize(8).internalBuffer().getDouble();
1350+
}
1351+
1352+
13221353
/**
13231354
* Compares this bytes instance to another.
13241355
* <p>

src/test/java/at/favre/lib/bytes/BytesToConvertOtherTypesTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,36 @@ public void toLong() throws Exception {
146146
} catch (UnsupportedOperationException e) {
147147
}
148148
}
149+
150+
@Test
151+
public void toFloat() throws Exception {
152+
assertEquals(1.0134550690550691E-17, Bytes.from(example_bytes_four).toFloat(), 0.001);
153+
154+
assertEquals(5.1E-322, Bytes.from(example_bytes_one).toFloat(), 0.001);
155+
assertEquals(3.3433E-320, Bytes.from(example_bytes_two).toFloat(), 0.001);
156+
assertEquals(0, Bytes.from(new byte[2]).toFloat(), 0);
157+
assertEquals(0, Bytes.from(new byte[4]).toFloat(), 0);
158+
159+
try {
160+
Bytes.from(new byte[5]).toFloat();
161+
fail();
162+
} catch (UnsupportedOperationException e) {
163+
}
164+
}
165+
166+
@Test
167+
public void toDouble() throws Exception {
168+
assertEquals(-6.659307728279082E225, Bytes.from(example_bytes_eight).toDouble(), 0.001);
169+
170+
assertEquals(5.1E-322, Bytes.from(example_bytes_one).toDouble(), 0.001);
171+
assertEquals(3.3433E-320, Bytes.from(example_bytes_two).toDouble(), 0.001);
172+
assertEquals(0, Bytes.from(new byte[4]).toDouble(), 0);
173+
assertEquals(0, Bytes.from(new byte[8]).toDouble(), 0);
174+
175+
try {
176+
Bytes.from(new byte[9]).toDouble();
177+
fail();
178+
} catch (UnsupportedOperationException e) {
179+
}
180+
}
149181
}

0 commit comments

Comments
 (0)