Re: SOLUTION: Insert a Euro symbol as UTF-8 from a latin1 charset.

From: "Jeroen T(dot) Vermeulen" <jtv(at)xs4all(dot)nl>
To: Roland Glenn McIntosh <roland(at)steeltorch(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SOLUTION: Insert a Euro symbol as UTF-8 from a latin1 charset.
Date: 2003-06-13 15:50:29
Message-ID: 20030613155029.GN31141@xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Jun 13, 2003 at 11:28:36AM -0400, Roland Glenn McIntosh wrote:
>
> The Euro symbol is unicode value 0x20AC. UTF-8 encoding is a way of representing most unicode characters in two bytes, and most latin characters in one byte.

More precisely, UTF-8 encodes ASCII characters in one byte. All other
latin-1 characters take 2 bytes IIRC, with the rest taking up to 4 bytes.

> I don't know why my "20" byte turned into two bytes of E2 and 82.

Haven't got the spec handy, but UTF-8 uses the most-significant bit(s) of
each byte as a "continuation" field. If the upper bit is zero, the char
is a plain 7-bit ASCII value. If it's 1, the byte is part of a multibyte
sequence with a few most-significant bits indicating the sequence's length
and the byte's position in it (IIRC it's something like a countdown to the
end of the sequence).

In a nutshell, you can't just take bits away from your Unicode value and
call it UTF-8; it's a variable-length encoding and it needs some extra
room for the length information to go.

Furthermore, I don't think the Euro symbol is in latin-1 at all. It was
added in latin-9 (iso 8859-15) and so it's not likely to have gotten a
retroactive spot in the bottom 256 character values. Hence it will take
UTF-8 more bytes to encode it.

> Furthermore, I was under the impression that a UTF-8 encoding of the Euro sign only took two bytes. Corroborating this assumption, upon dumping that table with pg_dump and examining the resultant file in a hex editor, I see this in that character position: AC 20

How does that "corroborate the assumption?" You're looking at the Unicode
value now, in a fixed-length 16-bit encoding.


> I did try the '\0x20AC' method, and '\0x20\0xAC' without success.
> It's worth noting that the field I'm inserting into is an SQL_ASCII field, and I'm reading my UTF-8 string out of it like this, via JDBC:

You can't fit UTF-8 into ASCII. UTF-8 is an eight-byte encoding; ASCII
is a 7-bit character set.

Jeroen

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2003-06-13 15:52:41 Re: [HACKERS] PostgreSQL libraries - PThread Support, but
Previous Message ohp 2003-06-13 15:43:53 Re: Mirro updates