-
Notifications
You must be signed in to change notification settings - Fork 18
/
ParseByteArray.java
46 lines (37 loc) · 1.54 KB
/
ParseByteArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package jp.android.a.akira.library.okwear.util;
import java.nio.ByteBuffer;
public class ParseByteArray {
public static byte[] fromString(final String value) {
return value.getBytes();
}
public static byte[] fromChar(final char value) {
final int arraySize = Character.SIZE / Byte.SIZE;
final ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putChar(value).array();
}
public static byte[] fromShort(final short value) {
final int arraySize = Short.SIZE / Byte.SIZE;
final ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putShort(value).array();
}
public static byte[] fromInt(final int value) {
final int arraySize = Integer.SIZE / Byte.SIZE;
final ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putInt(value).array();
}
public static byte[] fromLong(final long value) {
final int arraySize = Long.SIZE / Byte.SIZE;
final ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putLong(value).array();
}
public static byte[] fromFloat(final float value) {
final int arraySize = Float.SIZE / Byte.SIZE;
final ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putFloat(value).array();
}
public static byte[] fromDouble(final double value) {
int arraySize = Double.SIZE / Byte.SIZE;
ByteBuffer buffer = ByteBuffer.allocate(arraySize);
return buffer.putDouble(value).array();
}
}