Re: Knowing the length(convert(username using windows_1251_to_utf8))

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Knowing the length(convert(username using windows_1251_to_utf8))
Date: 2007-01-11 10:35:32
Message-ID: 20070111103532.GB5836@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 11, 2007 at 10:19:38AM +0100, Alexander Farber wrote:
> Hello PostgreSQL users!
>
> I have this data stored in WIN1251 encoding, which
> is being fetched by a libpq application I'm developing:

<snip>

> phpbb=> select username, length(username), length(convert(username
> using windows_1251_to_utf8)) from phpbb_users where user_id=224;
> username | length | length
> -----------------+--------+--------
> ????????? ?. ?. | 15 | 26
> (1 row)
>
> My problem is that I need the username in the utf8 encoding.
> So I use the convert(username using windows_1251_to_utf8)
> which works fine except one thing:

If you need the string in UTF-8, why not just set the "client_encoding"
to "utf8" and then the server will only send you strings in utf8, not
conversion necessary.

> Is there please a way to know the length of the utf8 data?
> (I'm using a fixed char array in my C program)

UTF-8 always variable length, I think up to 4 bytes per character.
Maybe you should n't be using a fixed-length array?

> How do you usually handle such cases?

Variable length arrays.

In your next email you ask:
> Can I still be sure that the data returned in the
> convert(username using windows_1251_to_utf8)
> column will be 0-terminated or should I fetch
> the data length using PQgetlength and maintain
> that value in my C-program?

In the client end (as long you're not doing binary transfers) the
strings are always null terminated.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andy Dale 2007-01-11 10:46:17 Re: Recording insert, updates, and deletes
Previous Message Hannes Dorbath 2007-01-11 10:31:45 Cluster all tables in database to PK index