假设我有一些定义为:的实用程序功能:
(defun write-bytes-to-file (bytes file-path bits)
(with-open-file (stream file-path
:direction :output
:if-does-not-exist :create
:if-exists :append
:element-type (list 'unsigned-byte bits))
(dolist (b bytes))
(write-byte b stream))))
(defun read-file-bytes-to-list (file-path bits)
(with-open-file (stream file-path :direction :input :element-type (list 'unsigned-byte bits))
(read-bytes-to-list stream nil)))
此外,假设我跑步:
(write-bytes-to-file '(65 65 65) "foo.txt" 8)
(write-bytes-to-file '(512) "foo.txt" 9)
现在这给我留下了一个可变的字节宽度文件,其中包含三个8位字节,另一个具有9位宽度。通常,我会使用read-file-bytes-to-list
函数和位宽输入来以正确的对齐方式读取文件,但是在这种情况下,我实际上无法做到这一点。有没有一种内置的方式可以更改Common LISP中的字节对齐方式?本质上,我想按预期方式读回整数(65、65、65和512)。谢谢你的帮助。我正在使用SBCL实现。
编辑:如果没有使用标准LISP库/实用工具来处理此问题的便捷方法,我意识到我很可能将不得不通过按位操作来处理此问题;没关系。我只是想知道在这样做之前是否有更好的方法。
lisp-binary
。>>我修改了您的书写功能以修复括号并打印实际的流元素类型: