【问题标题】:How to read utf-8 string by usocket如何通过 usocket 读取 utf-8 字符串
【发布时间】:2011-12-09 12:41:14
【问题描述】:

当我使用以下代码从 usocket 流中读取数据时:

(let ((stream (socket-stream sk)) line)
  (loop for line = (read-line stream)
     while line do (format t line)))

当 read-line 遇到非 ascii 字符时,它会抛出异常:

decoding error on stream
#<SB-SYS:FD-STREAM
  for "socket 118.229.141.195:52946, peer: 119.75.217.109..."
  {BCA02F1}>
(:EXTERNAL-FORMAT :UTF-8):
  the octet sequence (176) cannot be decoded.
   [Condition of type SB-INT:STREAM-DECODING-ERROR]

read-line 和 read-byte 都不起作用,所以我尝试使用 trivial-utf-8 来读取 utf-8 字符串 使用 read-utf-8-string,但它只接受二进制流,似乎 socket-stream 不创建二进制流,所以我很困惑如何从具有非 ascii 字符的套接字流中读取?

【问题讨论】:

    标签: networking lisp sbcl


    【解决方案1】:

    您可以先read-sequence(如果您提前知道长度)或read-bytes,然后使用(babel:octets-to-string octets :encoding :utf-8))(其中八位字节为(make-array expected-length :element-type '(unsigned-byte 8)))将它们转换为字符串。

    【讨论】:

    • 谢谢,我注意到iolib依赖于babel,所以我决定改用iolib,也许iolib可以比usocket更省力。
    【解决方案2】:

    您收到的错误表明您尝试读取的数据实际上不是有效的 UTF-8 数据。事实上,176 (= #b10110000) 不是一个可以引入 UTF-8 字符的字节。如果您尝试读取的数据采用其他编码,请尝试相应地调整 Lisp 编译器的外部格式设置或使用BabelFLEXI-STREAMS 来解码数据。

    【讨论】:

      【解决方案3】:

      一旦我需要它并且我懒得寻找一个库来做它,所以我自己做了:) 这可能不是最好的方法,但我只需要一些快速而不复杂的东西,所以在这里去:

      (defun read-utf8-char (stream)
        (loop for i from 7 downto 0
           with first-byte = (read-byte stream nil 0)
           do (when (= first-byte 0) (return +null+))
           do (when (or (not (logbitp i first-byte)) (= i 0))
                (setf first-byte (logand first-byte (- (ash 1 i) 1)))
                    (return
                  (code-char 
                   (dotimes (a (- 6 i) first-byte)
                     (setf first-byte
                           (+ (ash first-byte 6)
                              (logand (read-byte stream) #x3F)))))))))
      

      【讨论】:

        猜你喜欢
        • 2011-02-26
        • 2017-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        • 1970-01-01
        相关资源
        最近更新 更多