首页 > 解决方案 > Python 到 Java 代码:Java 中的 String.encode()

问题描述

我正在尝试使用 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中的文件?

标签: javapythonstringencode

解决方案


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

    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();
    }

谢谢!


推荐阅读