gpt4 book ai didi

java - 将 USB 键盘数据从字节数组转换为字符串 USB4Java

转载 作者:行者123 更新时间:2023-11-30 07:41:33 26 4
gpt4 key购买 nike

我正在使用 usb4java 读取 USB 键盘(二维码扫描仪)输入。

我的代码片段如下所示:

byte[] data = new byte[16];
UsbPipe usbPipe = usbEndpoint.getUsbPipe();
if (usbPipe != null) {
if (!usbPipe.isOpen()) {
usbPipe.open();
}
if (usbPipe.isOpen()) {
UsbIrp usbIrp = usbPipe.createUsbIrp();
usbIrp.setData(data);

我有两个问题:

1] 按A键,字节数组数据为2,0,0,0,0,0,0,0,2,0,4,0,0,0,0,0
按AB键,字节数组数据为2,0,0,0,0,0,0,0,2,0,4,0,0,0,0,0,2,0,5,0,0, 0,0,0

如何在java中将其转换为字符?即转换后得到 A 或 AB。

2] 目前,我在上面的代码片段中传递了固定大小的字节数组。例如,如果我期望 1 个字符,我将 16 作为字节数组的大小传递,对于 2 个字符,24 作为大小,依此类推。是否有任何其他优雅的解决方案可以使其动态化?

PS:我的字节数组转换器片段:

StringBuffer sb = new StringBuffer();
for (byte b : data) {
sb.append(b);
sb.append(",");
}
String byteString = sb.toString();
return byteString;

感谢您的帮助

编辑 1:完整源代码在这里:http://tpcg.io/zt3WfM

最佳答案

基于documentation格式应该是:

22 00 04 00 00 00 00 00
Offset  Size    Description
0 Byte Modifier keys status.
1 Byte Reserved field.
2 Byte Keypress #1.
3 Byte Keypress #2.
4 Byte Keypress #3.
5 Byte Keypress #4.
6 Byte Keypress #5.
7 Byte Keypress #6.

基于ASCII codes

// 'A' is 0x65
byte codeA = 0x04; // The code for A key
cahr a = 0x61 + codeA ;
byte codeX = 0x1B; // The code for X key
char x = 0x61 + code; // x == 'X'

System.out.println(a);
System.out.println(x);

或者你可以使用 Map(0x04, 'A')

关于java - 将 USB 键盘数据从字节数组转换为字符串 USB4Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55893051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com