Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run-time exception when attempting to print a Unicode character

Tags:

haskell

ghc

Char is the type for Unicode characters in Haskell, and String is simply [Char] (i.e. a list of Char items). Here is some simple code:

main = putStrLn "©" -- Unicode string

This code compiles fine, but I get the runtime exception when I run it in the PowerShel.exe or cmd.exe:

app.exe: : commitBuffer: invalid argument (invalid character)

Why does this happen? Weirdly enough, when I do the same in C#, I get no exception:

Console.WriteLine("©");

In .NET, chars are Unicode too. PowerShell or cmd prints c instead ©, but at least I get not exception. How can I get my Haskell executable to run smoothly?

like image 953
Andrey Bushman Avatar asked Dec 23 '14 08:12

Andrey Bushman


People also ask

What is Unicode error handling in Python?

In Python, Unicode standards have two types of error: Unicode encodes error and Unicode decode error. In Python, it includes the concept of Unicode error handlers. These handlers are invoked whenever a problem or error occurs in the process of encoding or decoding the string or given text. To include Unicode characters in ...

Can handle any Unicode code point?

Can handle any Unicode code point. A string of ASCII text is also valid UTF-8 text. UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.

What are unicodeencodeerror and unicodedecodeerror?

Strict error in Python raises UnicodeEncodeError and UnicodeDecodeError for encoding and decoding errors that are occurred, respectively. UnicodeEncodeError demonstration and its example. In Python, it cannot detect Unicode characters, and therefore it throws an encoding error as it cannot encode the given Unicode string.

How to write Unicode characters in Python?

Unicode characters in Python program can be written as follows: “u dfskgfkdsg”. Or. “U sakjhdxhj”. Or. “u1232hgdsa”. In the above syntax, we can see 3 different ways of declaring Unicode characters. In Python program we can write Unicode literals with prefix either “u” or “U” followed by a string containing alphabets and numerical ...


1 Answers

On Windows, the fix is to tell the shell to use code page 65001 (instructions here), which puts Windows in "UTF-8 mode". It's not perfect, but for most characters you should see unicode characters handled much better.

like image 141
bheklilr Avatar answered Sep 21 '22 19:09

bheklilr