2

当我使用以下代码从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 使用 read-utf-8-string 读取 utf-8 字符串,但它只接受二进制流,似乎 socket-stream 不创建一个二进制流,所以我很困惑如何从具有非 ascii 字符的套接字流中读取?

4

3 回答 3

1

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

于 2011-12-09T14:04:15.933 回答
1

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

于 2011-12-10T10:28:57.853 回答
0

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

(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-12-09T15:43:34.327 回答