Page 1 of 1

Unicode Chr()

Posted: Wed Aug 11, 2010 9:25 am
by bumblebee
Don't know if this has been mentioned before or not - I did have a quick look.
But anyway, I've suddenly (after all these years!) decided I need Unicode in my life!

In the process of creating a little code, I came across this problem, and work-around.
So I extracted it to the smallest amount of code I could, to demonstrate.

Please note for anyone who may find this post via a search engine, that you must enable the "Create Unicode Executable" in your compiler options in order for this code to work.

Code: Select all

; Note to self: Make sure you set the "Create Unicode Executable" option in the compiler options!
; setup
OpenWindow(1,10,10,400,200,"test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ListViewGadget(1,1,1,WindowWidth(1),WindowHeight(1))



; attempt 1.
AddGadgetItem(1,-1, Chr(1069)) ; display the ascii char "-"  not the unicode one. 

; attempt 2
txt.s=""
num.l=1069
txt = txt + Chr(num)
AddGadgetItem(1,-1, txt) ; this displays the unicode.



; wait for close window.
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
It seems to me that the Chr() code does not switch to the Wide Char version, or perhaps the literal value 1069 is truncated to a byte. Either way, we perhaps need a UChr() command, or for the current one to behave nicely with literals.

Re: Unicode Chr()

Posted: Wed Aug 11, 2010 9:30 am
by Trond
Chr(literal number) is optimized into a string on compile time. If your source file encoding is set to plain text, unicode characters will be truncated to ascii. Set your source file encoding to UTF-8 to prevent this problem.