sbcl:在没有实际I / O的情况下编码和解码字符 [英] sbcl: encoding and decoding characters without actual I/O

查看:71
本文介绍了sbcl:在没有实际I / O的情况下编码和解码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sbcl中,使用:utf-8编码字符串时,有没有一种方法可以将其编码为字节向量,而无需执行实际的I / O,类似于clisp的

In sbcl, when encoding a string using, say, :utf-8, is there a way to encode it to a byte vector without doing actual I/O, similar to clisp's

(EXT:CONVERT-STRING-TO-BYTES string encoding &KEY :START :END)

并使用诸如clisp之类的东西进行解码

and also decode with something like clisp's

(EXT:CONVERT-STRING-FROM-BYTES vector encoding &KEY :START :END)

我可以通过写粗略地近似将数据保存到具有所需编码的文件中,然后使用:iso-8859-1重新读取,但这似乎是一个笨拙的捷径。

I could crudely approximate this by writing the data to a file with the desired encoding and then rereading it using :iso-8859-1, but that seems to be a dorky longcut.

推荐答案

SBCL具有功能 SB-EXT:STRING-TO-OCTETS SB-EXT:OCTETS-TO-STRING

SBCL has the functions SB-EXT:STRING-TO-OCTETS and SB-EXT:OCTETS-TO-STRING for this.

CL-USER> (sb-ext:string-to-octets "fööbär" :external-format :utf-8)
#(102 195 182 195 182 98 195 164 114)
CL-USER> (sb-ext:string-to-octets "fööbär" :external-format :iso-8859-1)
#(102 246 246 98 228 114)
CL-USER> (sb-ext:octets-to-string ** :external-format :utf-8)
"fööbär"
CL-USER> (sb-ext:octets-to-string ** :external-format :iso-8859-1)
"fööbär"

这篇关于sbcl:在没有实际I / O的情况下编码和解码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆