Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Just starting out? Need help? Post your questions and find answers here.
kparadine
New User
New User
Posts: 6
Joined: Sun Sep 23, 2018 3:18 am

Convert a #PB_Ascii or #PB_UTF8 string to UCS2

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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"
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

PokeS() using #PB_Unicode.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
kparadine
New User
New User
Posts: 6
Joined: Sun Sep 23, 2018 3:18 am

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

Post by kparadine »

skywalk wrote:PokeS() using #PB_Unicode.
Thank you, that made things very straightforward.
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply