首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Common Lisp中将字节数组转换为字符串?

如何在Common Lisp中将字节数组转换为字符串?
EN

Stack Overflow用户
提问于 2009-03-01 16:44:26
回答 6查看 11.7K关注 0票数 24

我正在调用一个有趣的API,它返回一个字节数组,但我想要一个文本流。有没有一种简单的方法可以从字节数组中获取文本流?现在,我只是拼凑了一下:

代码语言:javascript
复制
(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的解决方案。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2009-03-01 18:56:39

FLEXI-STREAMS (http://weitz.de/flexi-streams/)具有可移植的转换功能

代码语言:javascript
复制
(flexi-streams:octets-to-string #(72 101 108 108 111) :external-format :utf-8)

=>

"Hello"

或者,如果你想要一个流:

代码语言:javascript
复制
(flexi-streams:make-flexi-stream
   (flexi-streams:make-in-memory-input-stream
      #(72 101 108 108 111))
   :external-format :utf-8)

将返回一个从byte-vector读取文本的流

票数 32
EN

Stack Overflow用户

发布于 2009-03-02 12:32:24

有两个可移植的库可用于此转换:

  • flexi-streams,已经在另一个答案中提到过。

这个库比较老,有更多的特性,特别是可扩展的streams.

  • Babel,,它是一个专门用于字符编码和解码的库

与flexi-streams相比,Babel的主要优势是速度。

为了获得最佳性能,如果Babel具有您需要的功能,请使用Babel,否则使用flexi-streams。低于说明速度差异的(有点不科学的)微基准。

在这个测试用例中,Babel比快337倍的,需要的内存少200倍。

代码语言:javascript
复制
(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.
|#
票数 25
EN

Stack Overflow用户

发布于 2009-03-04 15:25:22

如果您不必担心UTF-8编码(本质上,这意味着“纯ASCII"),则可以使用MAP:

(map 'string #'code-char #(72 101 108 108 111))

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/600070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档