Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-03

Ord of a UTF8 char

12 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

José Mejuto
to Antônio

Hello Lazarus-List,

Saturday, March 27, 2010, 6:05:35 AM, you wrote:

A> Ord('a') makes sense to the compiler, Ord('?') does not. Why?

Because 'a' is equal to UTF8 string #97 which is only one byte in length. The "?" is a multibyte UTF8 sequence, more than one byte.

You can put your source code as an ANSI codepage like ISO8859-1 or alike, but you will be in troubles with the LCL which expect everything in UTF8 code.

If you need the "ANSI" version you can code:

ord(UTF8ToISO88591('?')[1]);

But that's awful and error prone.

Please forget ANSI codepage world, so forget to work with characters over 127 as BYTEs. A character is a byte sequence of non fixed length (except in UTF32). If you need ANSI output like in windows console programs convert data just before being output and only to display the output.

Best regards, Jos?

Reply by email

Antônio
to Mattias Gaertner

I need to convert characters to numbers and I thought on a character table or something.

Ant?nio

2010/3/27 Mattias Gaertner :

Show quoted text 18 lines
On Sat, 27 Mar 2010 02:05:35 -0300 Ant?nio wrote: > Ord('a') makes sense to the compiler, Ord('?') does not. Why? Because '?' is a string - a multibyte character. I'm curious, why do you need that? Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Mattias Gaertner
to Antônio
I need to convert characters to numbers and I thought on a character table or something.

unit LCLProc has this UTF8 function:

UTF8CharacterToUnicode

the system unit has this:

Utf8ToUnicode

Do a find declaration to see the many UTF8 functions.

Mattias

Reply by email

Antônio
to Mattias Gaertner

How do I parse a utf8 string?

Ant?nio

2010/3/27 Mattias Gaertner :

Show quoted text 26 lines
On Sat, 27 Mar 2010 14:46:39 -0300 Ant?nio wrote: > I need to convert characters to numbers and I thought on a character > table ?or something. unit LCLProc has this UTF8 function: UTF8CharacterToUnicode the system unit has this: Utf8ToUnicode Do a find declaration to see the many UTF8 functions. Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Antônio
to Antônio

For example:

var
  i :integer;
  s :utf8string;
begin
  for i := 0 to utf8klength(s) do
  begin
     // What the meaning of s[i] at this position?
  end;
end;

Ant?nio

2010/3/27 Ant?nio :

Show quoted text 32 lines
How do I parse a utf8 string? Ant?nio 2010/3/27 Mattias Gaertner : > On Sat, 27 Mar 2010 14:46:39 -0300 > Ant?nio wrote: > >> I need to convert characters to numbers and I thought on a character >> table ?or something. > > unit LCLProc has this UTF8 function: > > UTF8CharacterToUnicode > > > the system unit has this: > > Utf8ToUnicode > > > Do a find declaration to see the many UTF8 functions. > > Mattias > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus >

Reply by email

Antônio
to Mattias Gaertner

How do I return the character value from a unicode table value?

Ant?nio

2010/3/27 Mattias Gaertner :

Show quoted text 35 lines
On Sat, 27 Mar 2010 15:24:45 -0300 Ant?nio wrote: > For example: > > var > ? i :integer; > ? s :utf8string; > begin > ? for i := 0 to utf8klength(s) do > ? begin > > ? ? ?// What the meaning of s[i] at this position? Makes no sense. s[i] is the i-th byte of the string. In UTF8 one character can have one to four bytes. > ? end; > end; Maybe this helps: http://wiki.lazarus.freepascal.org/LCL_Unicode_Support Mattias -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Antônio
to Antônio

Well, UTF8CharacterToUnicode informs the length of the character, so I can read it directly from the string.

Thanks.

Ant?nio

2010/3/27 Ant?nio :

Show quoted text 41 lines
How do I return the character value from a unicode table value? Ant?nio 2010/3/27 Mattias Gaertner : > On Sat, 27 Mar 2010 15:24:45 -0300 > Ant?nio wrote: > >> For example: >> >> var >> ? i :integer; >> ? s :utf8string; >> begin >> ? for i := 0 to utf8klength(s) do >> ? begin >> >> ? ? ?// What the meaning of s[i] at this position? > > Makes no sense. > s[i] is the i-th byte of the string. In UTF8 one character can have one > to four bytes. > > >> ? end; >> end; > > Maybe this helps: > > http://wiki.lazarus.freepascal.org/LCL_Unicode_Support > > > Mattias > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus >

Reply by email

Michael Schnell
to Antônio: Ord('a') makes sense to the compiler, Ord('?') does not. Why? Ant?nio
Ant?nio wrote
Ord('a') makes sense to the compiler, Ord('?') does not. Why?

In the Unicode world a non-ASCII-character is not an 8 bit entity. So, with the current compiler version, (unexpectedly) a single quoted non-ASCII character is an UTF-8 coded string, while a single quoted ASCII character is a character. (I feel this might change with a future compiler version when D2010 strings might be introduced).

Unless you want to use the old style (depreciated) locale-dependent 8 bit ANSI encoding you could enumerate characters as 16 (or even 32 Bit) Unicode numbers. AFAIK appropriate conversion functions are available in the RTL.

-Michael

Reply by email