Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

How to convert Unicode Escape Sequence to Unicode String in Haskell

I have a string like "36193657363436093648359236573648362136573591" which I want to decode it. I tried search the unicode library without success.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Prelude> putStrLn "36193657363436093648359236573648362136573591"
???????????

Note that you don't actually have the string "36193657363436093648359236573648362136573591" – rather, you have the UTF-32 string ???????????, for which "36193657..." happens to be a ASCII-compliant literal. By default, GHCi uses the Show instance to display results, which doesn't so much show things as spit out literals that can be used as Haskell code for the thing. It's conservative in terms of unicode. That's why

Prelude> "???????????"
"36193657363436093648359236573648362136573591"

On the other hand, the putStrLn, putChar, hPutStr etc. functions will just dump the string itself in UTF-8 rather than an ASCII-safe representation thereof.

If you're actually reading the escaped string from a file or something, you can simply read it:

Prelude> s <?- getLine
"36193657363436093648359236573648362136573591"
Prelude> s
""\3619\3657\3634\3609\3648\3592\3657\3648\3621\3657\3591""
-- Note double escaping, because I'm showing a string that contains a string literal.
Prelude> putStrLn $ read s
???????????


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...