Page 1 of 1

Unicode escaped string to string

Posted: Fri Jul 30, 2010 7:16 pm
by Joakim Christiansen
This is for strings containing escaped unicode characters, for example "H\u00e5vard T\u00f8nnessen".
If compiled as unicode all unicode characters will be converted, but if compiled as ASCII only the unicode characters that can fit within the extended ASCII range (0-255) will be converted. Which means code for Æ Ø and Å will convert fine.

Code: Select all

EnableExplicit

Procedure.s uEscapedToString(string$) ;can be compiled as both ASCII and Unicode
  Protected len, pos, hex$, result$, unicode.c, char$, uChar$ = Space(1)
  len = Len(string$)
  For pos=1 To len
    char$ = Mid(string$,pos,1)
    If char$ = "\" And Mid(string$,pos+1,1) = "u"
      hex$ = Mid(string$,pos+2,4)
      If #PB_Compiler_Unicode=#False And Left(hex$,2) <> "00" ;this char can't fit within the extended ASCII table
        result$ + "?"
      Else
        unicode = Val("$"+hex$) ;the returned quad truncates fine
        PokeC(@uChar$,unicode)
        result$ + uChar$
      EndIf
      pos + 5
    Else
      result$ + char$
    EndIf
  Next
  ProcedureReturn result$
EndProcedure

Debug uEscapedToString("R\u00f8stum")
Remember, sharing is caring!