Python到Java代码:java中的String.encode()

问题描述 投票:0回答:1

我正在尝试使用Raspberry Pi模拟键盘,并在网上找到了实现该功能的一些代码。在python的基础上,它可以运行,但是现在我想将其转换为Java(我的主要语言),以便更多地使用它。

这是python代码的重要部分:

def write_report(report):
    print(report.encode())
    # with open('/dev/hidg0', 'rb+') as fd:
        # fd.write(report.encode())

# Press a
write_report(NULL_CHAR*2+chr(4)+NULL_CHAR*5)

因此输出如下:b'\x00\x00\x04\x00\x00\x00\x00\x00'

如何实现将相同的输出写入到Java文件中?

java python string encode
1个回答
0
投票

我找到了将相同字节写入文件的方法。这是代码:

    byte[] bytes = new byte[] {0,0,4,0,0,0,0,0};
    FileOutputStream fWriter;
    try {
        fWriter = new FileOutputStream(new File("D:\\testJava.txt"));
        fWriter.write(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.