Java:自定义到字节的转换
Java: custom to byte conversions
我正在使用一些低容量模块,我需要尽可能地压缩数据。数据将如下所示:
设备事件:
- 1 字节:
- 2 位状态(每次 00)
- RGB 颜色为 6 位(3 x 2 位)
- 2 字节:从现在到某个日期时间的分钟数
我需要为转换创建一个构造函数(最好是2个构造函数)from/to:
事件:
- byte[] 颜色(rgb,颜色将简化为只有 64 种可用)
- 一些日期时间(但我会得到以分钟为单位的整数差值,它足够小以适合两位)
所以基本上我需要:
- byte[3] 颜色 <-> 1 字节状态和颜色
- int 分钟 <-> byte[2]
分钟
如有任何帮助,我将不胜感激
我不太确定你的问题是什么,这可能会有所帮助:
final byte red = 1; // 01 binary
final byte green = 2; // 10 binary
final byte blue = 3; // 11 binary
final byte finalColor = (byte) ((red & 0x3) << 4) | ((green & 0x3) << 2) | (blue & 0x3);
System.out.println(finalColor);// finalColor is 011011 = 27 decimal
final int minutes = 0x1234; // first byte is 0x12, second byte is 0x34
final byte[] bytes = {(byte) (((minutes) >>> 8) & 0xff), (byte) (minutes & 0xff)};
System.out.println(bytes[0]); // 0x12 = 18 decimal
System.out.println(bytes[1]); // 0x34 = 52 decimal
我不确定第二个问题是什么。所以我做了这两个可能对你有帮助的功能:
public static int convertToInt(int a, int b, int c, int d) {
a = Math.min(a, 255);
b = Math.min(b, 255);
c = Math.min(c, 255);
d = Math.min(d, 255);
return ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | (d & 0xFF);
}
public static int[] extractComponents(int data) {
int a = (data >> 24) & 0xFF;
int b = (data >> 16) & 0xFF;
int c = (data >> 8) & 0xFF;
int d = data & 0xFF;
return new int[] {a, b, c, d};
}
convertToInt
函数接受四个数字(小于 255)并将它们全部放在一个 int 中。
extractComponents
函数做相反的事情。
这是一个例子:
int data = 0xC8E0601B;
int[] dataA = extractComponents(data);
for(int i = 0 ; i < dataA.length; i++) System.out.printf("%x\n", dataA[i]);
System.out.printf("%x\n", convertToInt(dataA[0], dataA[1], dataA[2], dataA[3]));
我正在使用一些低容量模块,我需要尽可能地压缩数据。数据将如下所示:
设备事件:
- 1 字节:
- 2 位状态(每次 00)
- RGB 颜色为 6 位(3 x 2 位)
- 2 字节:从现在到某个日期时间的分钟数
我需要为转换创建一个构造函数(最好是2个构造函数)from/to:
事件:
- byte[] 颜色(rgb,颜色将简化为只有 64 种可用)
- 一些日期时间(但我会得到以分钟为单位的整数差值,它足够小以适合两位)
所以基本上我需要:
- byte[3] 颜色 <-> 1 字节状态和颜色
- int 分钟 <-> byte[2] 分钟
如有任何帮助,我将不胜感激
我不太确定你的问题是什么,这可能会有所帮助:
final byte red = 1; // 01 binary
final byte green = 2; // 10 binary
final byte blue = 3; // 11 binary
final byte finalColor = (byte) ((red & 0x3) << 4) | ((green & 0x3) << 2) | (blue & 0x3);
System.out.println(finalColor);// finalColor is 011011 = 27 decimal
final int minutes = 0x1234; // first byte is 0x12, second byte is 0x34
final byte[] bytes = {(byte) (((minutes) >>> 8) & 0xff), (byte) (minutes & 0xff)};
System.out.println(bytes[0]); // 0x12 = 18 decimal
System.out.println(bytes[1]); // 0x34 = 52 decimal
我不确定第二个问题是什么。所以我做了这两个可能对你有帮助的功能:
public static int convertToInt(int a, int b, int c, int d) {
a = Math.min(a, 255);
b = Math.min(b, 255);
c = Math.min(c, 255);
d = Math.min(d, 255);
return ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | (d & 0xFF);
}
public static int[] extractComponents(int data) {
int a = (data >> 24) & 0xFF;
int b = (data >> 16) & 0xFF;
int c = (data >> 8) & 0xFF;
int d = data & 0xFF;
return new int[] {a, b, c, d};
}
convertToInt
函数接受四个数字(小于 255)并将它们全部放在一个 int 中。
extractComponents
函数做相反的事情。
这是一个例子:
int data = 0xC8E0601B;
int[] dataA = extractComponents(data);
for(int i = 0 ; i < dataA.length; i++) System.out.printf("%x\n", dataA[i]);
System.out.printf("%x\n", convertToInt(dataA[0], dataA[1], dataA[2], dataA[3]));