如何在 Common Lisp 中将字节数组转换为字符串?
我正在调用一个有趣的 API,它返回一个字节数组,但我想要一个文本流。 有没有一种简单的方法可以从字节数组中获取文本流? 现在我只是把: 放在一起,
(defun bytearray-to-string (bytes)
(let ((str (make-string (length bytes))))
(loop for byte across bytes
for i from 0
do (setf (aref str i) (code-char byte)))
str))
然后将结果包装在 with-input-from-string 中,但这不是最好的方法。 (另外,它的效率非常低。)
在这种情况下,我知道它始终是 ASCII,因此将其解释为 ASCII 或 UTF-8 就可以了。 我正在使用支持 Unicode 的 SBCL,但我更喜欢一种可移植(甚至仅限 ASCII)的解决方案,而不是特定于 SBCL-Unicode 的解决方案。
I'm calling a funny API that returns a byte array, but I want a text stream. Is there an easy way to get a text stream from a byte array? For now I just threw together:
(defun bytearray-to-string (bytes)
(let ((str (make-string (length bytes))))
(loop for byte across bytes
for i from 0
do (setf (aref str i) (code-char byte)))
str))
and then wrap the result in with-input-from-string, but that can't be the best way. (Plus, it's horribly inefficient.)
In this case, I know it's always ASCII, so interpreting it as either ASCII or UTF-8 would be fine. I'm using Unicode-aware SBCL, but I'd prefer a portable (even ASCII-only) solution to a SBCL-Unicode-specific one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有两个用于此转换的可移植库:
flexi-streams,已在另一个答案中提到。
这个库比较旧,有更多功能,特别是可扩展流。
Babel,专门用于字符编码和解码的库
Babel 相对于 Flexi-streams 的主要优势是速度。
为了获得最佳性能,如果 Babel 具有您需要的功能,请使用它,否则请使用 Flexi-streams。 下面是一个(有点不科学的)微基准测试,说明了速度差异。
对于此测试用例,Babel 的速度快了 337 倍,并且需要的内存减少了 200 倍。
There are two portable libraries for this conversion:
flexi-streams, already mentioned in another answer.
This library is older and has more features, in particular the extensible streams.
Babel, a library specificially for character encoding and decoding
The main advantage of Babel over flexi-streams is speed.
For best performance, use Babel if it has the features you need, and fall back to flexi-streams otherwise. Below a (slighly unscientific) microbenchmark illustrating the speed difference.
For this test case, Babel is 337 times faster and needs 200 times less memory.
如果您不必担心 UTF-8 编码(本质上,这意味着“只是纯 ASCII”),您也许可以使用 MAP:
If you don't have to worry about UTF-8 encoding (that, essentially, means "just plain ASCII"), you may be able to use MAP:
我建议采用建议的 Flexistream 或 babel 解决方案。
但为了完整性和未来到达此页面的谷歌用户的利益,我想提一下 sbcl 自己的 sb-ext:octets-to-string:
I say go with the proposed flexistream or babel solutions.
But just for completeness and the benefit of future googlers arriving at this page I want to mention sbcl's own sb-ext:octets-to-string:
SBCL 支持所谓的灰色流。 这些是基于 CLOS 类和通用函数的可扩展流。 您可以创建一个文本流子类来从字节数组中获取字符。
SBCL supports the so-called Gray Streams. These are extensible streams based on CLOS classes and generic functions. You could create a text stream subclass that gets the characters from the byte array.
尝试使用
FORMAT
功能。(FORMAT NIL ...)
以字符串形式返回结果。Try the
FORMAT
function.(FORMAT NIL ...)
returns the results as a string.FLEXI-STREAMS (http://weitz.de/flexi-streams/) 具有便携式转换功能
或,如果你想要一个流:
将返回一个从字节向量读取文本的流
FLEXI-STREAMS (http://weitz.de/flexi-streams/) has portable conversion function
Or, if you want a stream:
will return a stream that reads the text from byte-vector