在 Latin1 编码的 Data.ByteString 和 Data.Text 之间转换
由于 latin-1(又名 ISO-8859-1)字符集作为最低 256 个代码点嵌入到 Unicode 字符集中,因此我希望转换很简单,但我没有看到任何 latin-1 Data.Text.Encoding
仅包含常见 UTF 编码的转换函数。
在以 latin-1 表示形式编码的 Data.ByteString
值和 Data.Text
值之间进行转换的推荐和/或有效方法是什么?
Since the latin-1 (aka ISO-8859-1) character set is embedded in the Unicode character set as its lowest 256 code-points, I'd expect the conversion to be trivial, but I didn't see any latin-1 encoding conversion functions in Data.Text.Encoding
which contains only conversion functions for the common UTF encodings.
What's the recommended and/or efficient way to convert between Data.ByteString
values encoded in latin-1 representation and Data.Text
values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案就在您链接的页面顶部:
一个快速的 GHCi 示例:
但是,正如您所指出的,在 latin-1 的特定情况下,代码点与 Unicode 一致,因此你可以使用从
Data.ByteString.Char8
中进行pack
/unpack
以执行从 latin-1 到String
的简单映射,然后您可以使用Data.Text
中相应的pack
/unpack
将其转换为Text
。The answer is right at the top of the page you linked:
A quick GHCi example:
However, as you pointed out, in the specific case of latin-1, the code points coincide with Unicode, so you can use
pack
/unpack
fromData.ByteString.Char8
to perform the trivial mapping from latin-1 from/toString
, which you can then convert toText
using the correspondingpack
/unpack
fromData.Text
.