Recent

Author Topic: Non ASCII WideChar constants  (Read 9074 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Non ASCII WideChar constants
« on: November 15, 2009, 12:49:56 pm »
Can't I declare wide char constants?

I tried this:

const
  C = WideChar('Š'); // this generates a compile time error

Compiler could not compile it, with message "Error: Illegal expression"


However, more strange to me, the following compiles:

const
  C = WideChar('A'); // this is OK, but only for letters whose ASCII value is less than 127
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Non ASCII WideChar constants
« Reply #1 on: November 15, 2009, 01:09:54 pm »
Lazarus is using UTF-8.

What do you want to achieve?
http://catb.org/~esr/faqs/smart-questions.html#goal

zeljko

  • Hero Member
  • *****
  • Posts: 1924
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Non ASCII WideChar constants
« Reply #2 on: November 15, 2009, 01:35:56 pm »
@Zoran,
I've had such problems but found easy solution (if I have similar problem) :)

resourcestring
 rsSHM = 'š';
 rsSHV = 'Š';
 rsZHM = 'ž';
 rsZHV = 'Ž';
 rsCHM = 'č';
 rsCHV = 'Č';
 rsTJM = 'ć';
 rsTJV = 'Ć';
etc etc...(other ISO8859-2/cp1250 non asci chars).

eg.
MyStr := 'Dobra ve'+rsCHM+'er';

hope it helps :)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Non ASCII WideChar constants
« Reply #3 on: November 15, 2009, 01:44:58 pm »
@Zeljko: Which problem does this solve?

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Non ASCII WideChar constants
« Reply #4 on: November 15, 2009, 03:06:19 pm »
Thank you, Željko.

Theo, I must admit that I really had not explained my goal clearly, so I'm sorry and now I apologize.

Actually, I managed to achieve what I want to do with WideStrings instead of WideChar, without WideChar constants, only using WideString variables.

However, I didn't intend just to ask for help for particular problem, I actually wanted to understand the usage of WideChar, the behavior that appears strange to me.
I believe that many times in future I could find using WideChar constants an easier and nicer way to do something.
I didn't mean to get help for just some piece of code I'm working on now.

Sorry, now I see that I should have said exactly what I want. I didn't start this topic the best way, so I appologize.

@Zeljko: Which problem does this solve?


Theo, you had already made your point before. Željko gave his solution for what he assumed to be my problem and that was the best thing one could do, wasn't it?
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Non ASCII WideChar constants
« Reply #5 on: November 15, 2009, 03:17:31 pm »
Theo, you had already made your point before. Željko gave his solution for what he assumed to be my problem and that was the best thing one could do, wasn't it?

I don't think so and I still don't understand which problem this solves.
Can you do WideChar(rsSHM); with this? I guess not.
Plus I don't think that you understand now what it's all about.

My answer to your first question would have been:

var C:WideChar; 

C := UTF8Decode('Š')[1];
And back:
Caption:=UTF8Encode(C);

But I first wanted to know what you need.
« Last Edit: November 15, 2009, 03:31:09 pm by theo »

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Non ASCII WideChar constants
« Reply #6 on: November 15, 2009, 03:46:14 pm »
Theo, I solved what i wanted to do and my actual code is really not important.
When I saw that compiler has a problem with WideChar constants, I just wanted to clear what the problem with that is, what can be wrong there. I didn't mean to ask for alternative solutions about getting my code to work, without using WideChar constants.

I should have said that at first place, so this missunderstanding is my fault.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

zeljko

  • Hero Member
  • *****
  • Posts: 1924
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Non ASCII WideChar constants
« Reply #7 on: November 15, 2009, 09:40:39 pm »
My answer to your first question would have been:

var C:WideChar; 

C := UTF8Decode('Š')[1];
And back:
Caption:=UTF8Encode(C);

But I first wanted to know what you need.

@theo: I've just misinterpreted Zoran's question, since an char/widechar 'Š' as constant is just a thing which can be used for string ops.
Never seen any other usage of such const ... so that was the reason of my answer. :)



 

TinyPortal © 2005-2018