使用 Haskell 输出 UTF-8 编码的 ByteString [英] Using Haskell to output a UTF-8-encoded ByteString
问题描述
我想简单地将 UTF-8 编码的数据输出到控制台,我快疯了.
I'm going out of my mind trying to simply output UTF-8-encoded data to the console.
我已经使用 String
成功地完成了这项工作,但现在我想用 ByteString
来做同样的事情.有没有好的快速的方法来做到这一点?
I've managed to accomplish this using String
, but now I'd like to do the same with ByteString
. Is there a nice and fast way to do this?
这是我到目前为止所得到的,但它不起作用:
This is what I've got so far, and it's not working:
import Prelude hiding (putStr)
import Data.ByteString.Char8 (putStr, pack)
main :: IO ()
main = putStr $ pack "čušpajž日本語"
它打印出uapaj~ ,
,呃.
我想知道最新的 GHC 6.12.1 的最佳答案,尽管我也想听听以前版本的答案.
I'd like an answer for the newest GHC 6.12.1 best, although I'd like to hear answers for previous versions as well.
谢谢!
更新:简单地读取和输出相同的 UTF-8 编码文本行似乎可以正常工作.(使用 Data.ByteString.Char8
,我只做了一个 putStr =<< getLine
.)但是从 .hs 文件中打包值,如上例所示,拒绝正常输出...我一定是做错了什么?
Update: Simply reading and outputting the same UTF-8-encoded line of text seems to work correctly. (Using Data.ByteString.Char8
, I just do a putStr =<< getLine
.) But packed values from inside the .hs file, as in the above example, refuse to output properly... I must be doing something wrong?
推荐答案
utf8-string
支持字节串.
import Prelude hiding (putStr)
import Data.ByteString.Char8 (putStr)
import Data.ByteString.UTF8 (fromString)
main :: IO ()
main = putStr $ fromString "čušpajž日本語"
这篇关于使用 Haskell 输出 UTF-8 编码的 ByteString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!