Page 1 of 1

chr() generating wrong character?

Posted: Mon Jul 17, 2023 10:20 pm
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

Re: chr() generating wrong character?

Posted: Wed Aug 09, 2023 5:36 pm
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.

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

Posted: Wed Aug 09, 2023 8:57 pm
by nsstudios
Thank you.

Re: chr() generating wrong character?

Posted: Thu Aug 10, 2023 11:01 am
by idle
If you need utf16 you can use this module
https://dnscope.io/idlefiles/UTF16.pb

Re: chr() generating wrong character?

Posted: Thu Aug 10, 2023 1:37 pm
by nsstudios
Thank you.