Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am using function Char and Chr with parameter 130 and has behave different in Delphi 2007 e XE3.
Delphi 2007: Char( 130 ) or Chr( 130 ) - Result: Comma
Delphi XE3: Char( 130 ) or Chr( 130 ) - Result: #$0082
I problem resolve using AnsiChar( 130 ).
My question is if I have problems using the AnsiChar. Should I change Chr / Char by AnsiChar throughout the project?
Thanks
–
Char(130) is a
value typecast
rather than a function call. An integer with value
130
is converted to a
Char
with that ordinal.
Chr(130) is indeed a function call. It invokes the
intrinsic
function
Chr()
.
In both pre and post Unicode versions of Delphi, you can use the
Char()
and
Chr()
versions interchangeably. However, the results differ depending on the Delphi version you use.
For pre-Unicode Delphi,
Char
is an 8 bit ANSI character. For post-Unicode Delphi,
Char
is a 16 bit UTF-16 character.
Exactly how you should resolve this depends on what you are trying to achieve. If you wish to bury your head in the sand, and pretend that Unicode characters don't exist, then perhaps you want to replace all your use of
Char
with
AnsiChar
. And you'll also want to hope that your program is only ever run on a machine with a locale that maps character 130 onto that character. Do be aware that not all Windows ANSI locales do so.
However, I suspect that the right solution to your problem, whatever it is, will be to embrace Unicode, and use the UTF-16 encoding for that character. It is
SINGLE LOW-9 QUOTATION MARK U+201A
. Write it like this in Unicode Delphi:
Chr($201A)
or like this:
#$201A
or like this:
On the other hand, perhaps you actually do want a comma, noting that AnsiChar(130)
is not a comma but is in fact a quotation mark.
If you want a comma (COMMA U+002C), well that's easy:
Chr($002C)
#$002C
Some required reading for you: Delphi and Unicode by Marco Cantù.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.