使用Haskell输出一个UTF-8编码的ByteString [英] Using Haskell to output a UTF-8-encoded ByteString

查看:140
本文介绍了使用Haskell输出一个UTF-8编码的ByteString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经设法使用<$来实现这个功能了。

我试图简单地将UTF-8编码的数据输出到控制台。 c $ c> String ,但现在我想对 ByteString 做同样的事情。有没有一种快捷的方式来做到这一点?



这是我到目前为止所做的,并且不起作用:

  import Prelude隐藏(putStr)
导入Data.ByteString.Char8(putStr,pack)
$ b $ main :: IO()
main = putStr $ packčušpajž日本语

打印出 uapaj〜 , ,ugh。



我最喜欢最新的GHC 6.12.1的答案, d也喜欢听到以前版本的答案。

谢谢!

更新:只需读取并输出相同的UTF-8编码的文本行似乎正常工作。 (使用 Data.ByteString.Char8 ,我只是做一个 putStr =<< getLine 。)但是打包值从.hs文件中,如上面的例子中,拒绝输出正确...我一定是做错了什么?

解决方案

<

  utf8-string  import prelude hiding(putStr )
import Data.ByteString.Char8(putStr)
import Data.ByteString.UTF8(fromString)

main :: IO()
main = putStr $ fromString čušpajž日本语


I'm going out of my mind trying to simply output UTF-8-encoded data to the console.

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ž日本語"

It prints out uapaj~�,�, ugh.

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.

Thanks!

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 supports bytestrings.

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屋!

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