java - 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中的文件?
解决方案
我找到了一种将相同字节写入文件的方法。这是代码:
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();
}
谢谢!
推荐阅读
- django - 在 Nginx 上与 Daphne 一起运行 Gunicorn
- etl - Stream/Task 是否可以用作仓库解决方案中的 ETL
- scikit-learn - 高斯过程回归器 - 所有值 sklearn 0.23 版本的相同标准偏差
- postgresql - 这是 SQL 注入吗
- c# - C# 引用类型在方法中存在多长时间?
- android - ffmpeg 需要运行这个命令 360 次
- python - 姜戈。在 ckeditor 编辑器中显示 HTML 元素
- json - 自定义 Api 响应并稍后在其他 React 组件上使用它
- sql - 如何优化这个 sql server 更新请求
- java - 一个非常简单的案例在 IBM i 中不使用 itext 写入 pdf 文件