我正在调用一个有趣的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的解决方案。
发布于 2009-03-01 18:56:39
FLEXI-STREAMS (http://weitz.de/flexi-streams/)具有可移植的转换功能
(flexi-streams:octets-to-string #(72 101 108 108 111) :external-format :utf-8)
=>
"Hello"或者,如果你想要一个流:
(flexi-streams:make-flexi-stream
(flexi-streams:make-in-memory-input-stream
#(72 101 108 108 111))
:external-format :utf-8)将返回一个从byte-vector读取文本的流
发布于 2009-03-02 12:32:24
有两个可移植的库可用于此转换:
这个库比较老,有更多的特性,特别是可扩展的streams.
与flexi-streams相比,Babel的主要优势是速度。
为了获得最佳性能,如果Babel具有您需要的功能,请使用Babel,否则使用flexi-streams。低于说明速度差异的(有点不科学的)微基准。
在这个测试用例中,Babel比快337倍的,需要的内存少200倍。
(asdf:operate 'asdf:load-op :flexi-streams)
(asdf:operate 'asdf:load-op :babel)
(defun flexi-streams-test (bytes n)
(loop
repeat n
collect (flexi-streams:octets-to-string bytes :external-format :utf-8)))
(defun babel-test (bytes n)
(loop
repeat n
collect (babel:octets-to-string bytes :encoding :utf-8)))
(defun test (&optional (data #(72 101 108 108 111))
(n 10000))
(let* ((ub8-vector (coerce data '(simple-array (unsigned-byte 8) (*))))
(result1 (time (flexi-streams-test ub8-vector n)))
(result2 (time (babel-test ub8-vector n))))
(assert (equal result1 result2))))
#|
CL-USER> (test)
Evaluation took:
1.348 seconds of real time
1.328083 seconds of user run time
0.020002 seconds of system run time
[Run times include 0.12 seconds GC run time.]
0 calls to %EVAL
0 page faults and
126,402,160 bytes consed.
Evaluation took:
0.004 seconds of real time
0.004 seconds of user run time
0.0 seconds of system run time
0 calls to %EVAL
0 page faults and
635,232 bytes consed.
|#发布于 2009-03-04 15:25:22
如果您不必担心UTF-8编码(本质上,这意味着“纯ASCII"),则可以使用MAP:
(map 'string #'code-char #(72 101 108 108 111))
https://stackoverflow.com/questions/600070
复制相似问题