Page 1 of 1

Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Posted: Thu Apr 08, 2021 9:09 pm
by kparadine
Is there an easy way to do this string conversion?

I have found Ascii() and UTF8() but not something to go the other way.

Re: Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Posted: Thu Apr 08, 2021 9:13 pm
by STARGĂ…TE
UCS2 is from 0 to $FFFF equal to UTF-16 which is used by PB internally.
There is no need for UCS2() because you can simply use the pointer to the unicode-string "@string"

Re: Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Posted: Thu Apr 08, 2021 9:16 pm
by skywalk
PokeS() using #PB_Unicode.

Re: Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Posted: Fri Apr 09, 2021 1:00 am
by kparadine
skywalk wrote:PokeS() using #PB_Unicode.
Thank you, that made things very straightforward.

Re: Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Posted: Fri Apr 09, 2021 8:33 am
by mk-soft

Code: Select all

;-TOP by mk-soft

CompilerIf #PB_Compiler_Version < 550
  
  Procedure Ascii(String.s)
    Protected *memory
    *memory = AllocateMemory(StringByteLength(String, #PB_Ascii) + 1)
    If *memory
      PokeS(*memory, String, -1, #PB_Ascii)
    EndIf
    ProcedureReturn *memory
  EndProcedure
  
  Procedure UTF8(String.s)
    Protected *memory
    *memory = AllocateMemory(StringByteLength(String, #PB_UTF8) + 1)
    If *memory
      PokeS(*memory, String, -1, #PB_UTF8)
    EndIf
    ProcedureReturn *memory
  EndProcedure
  
CompilerEndIf

Procedure Unicode(String.s)
  Protected *memory
  *memory = AllocateMemory(StringByteLength(String, #PB_Unicode) + 2)
  If *memory
    PokeS(*memory, String, -1, #PB_Unicode)
  EndIf
  ProcedureReturn *memory
EndProcedure

;-;(
text.s = "Hello World!"
*mem = Unicode(text)
Debug PeekS(*mem)
FreeMemory(*mem)