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

Mailing lists / lazarus / 2010-03
12 messages · 4 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Ord('a') makes sense to the compiler, Ord('?') does not. Why?
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.
Ord('a') makes sense to the compiler, Ord('?') does not. Why? Ant?nio
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
I need to convert characters to numbers and I thought on a character table or something.
Ant?nio
2010/3/27 Mattias Gaertner :
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
How do I parse a utf8 string?
Ant?nio
2010/3/27 Mattias Gaertner :
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 :
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
How do I return the character value from a unicode table value?
Ant?nio
2010/3/27 Mattias Gaertner :
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 :
Ord('a') makes sense to the compiler, Ord('?') does not. Why? Ant?nio
Ant?nio wroteOrd('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
For example: var i :integer; s :utf8string; begin for i := 0 to utf8klength(s) do begin…
Ant?nio wrote// What the meaning of s[i] at this position?
Nothing that makes sense unless you want to deal with the internals of UTF-8 coding.
IMHO you better use the appropriate RTL function to convert the string to a 32 bit Unicode string. Now you can do
c32 := s32[i];-Michael