chr() generating wrong character?

Just starting out? Need help? Post your questions and find answers here.
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

chr() generating wrong character?

Post by nsstudios »

Greetings,

Came across this potential bug.
As you'll hopefully be able to reproduce, when the calculation is put directly inside chr(), it generates two characters instead of one, but putting the calculation into a unicode .u variable first and then calling chr() on it works fine.
Tested with 6.01 lts (64/32 bit, c/asm), and 6.03 beta 3 (windows 64/32 bit, c/asm).

Code: Select all

EnableExplicit
Define a.u=(((120000-65536)>>9)+55192)
Debug a; 55298
Define c$=Chr(a)
Debug c$; character 55298 (0xd802)
Debug Len(c$); 1
Define u=PeekU(@c$)
Debug ""+u+" (0x"+Hex(u, #PB_Unicode)+")"
Debug (((120000-65536)>>9)+55192); 55298
c$=Chr((((120000-65536)>>9)+55192))
Debug c$; two 65553 characters (0xfffd)
Debug Len(c$); 2
u=PeekU(@c$)
Debug ""+u+" (0x"+Hex(u, #PB_Unicode)+")"
u=PeekU(@c$+SizeOf(character))
Debug ""+u+" (0x"+Hex(u, #PB_Unicode)+")"
Debug Bin(a, #PB_Unicode); 1101100000000010
Debug Bin((((120000-65536)>>9)+55192), #PB_Unicode); 1101100000000010
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: chr() generating wrong character?

Post by Fred »

Good catch. The value you are using is not a BMP valid character (UCS-2 ranges are 0x0000 to 0xD7FF and 0xE000 to 0xFFFF as specified here https://en.wikipedia.org/wiki/UTF-16#Co ... o_U+10FFFF). I corrected the Chr() command so it returns an empty string
if the character value is not valid so we get consistant result. Added a runtime debugger check and doc is updated as well.
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: [Done] chr() generating wrong character?

Post by nsstudios »

Thank you.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: chr() generating wrong character?

Post by idle »

If you need utf16 you can use this module
https://dnscope.io/idlefiles/UTF16.pb
nsstudios
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Aug 28, 2019 1:01 pm
Location: Serbia
Contact:

Re: chr() generating wrong character?

Post by nsstudios »

Thank you.
Post Reply