Page 1 of 1

Unicode to UTF8

Posted: Thu Mar 23, 2023 12:04 am
by reinaldocrespo
Hello FWh!

Data coming from some webservices to my webhook service is arriving as Unicode.

for example: Goyt\u00eda is Unicode which translated to UTF8 would become Goytía and that is what I need to display.

I tried hb_Translate( 'Goyt\u00eda', "ES850", "UTF8" ) but that is not quite doing the trick.

Can someone here help?

Thank you,

Reinaldo.

Re: Unicode to UTF8

Posted: Thu Mar 23, 2023 3:42 am
by nageswaragunupudi
"00eb" is UTF16BE character for ANSI "í". ( BE = Big Endian )
"Goyt_a" is ANSI string, not UTF8.
So, we need to convert the UTF16 char "\u00EB" to ANSI not UTF8.

Code: Select all | Expand

function myfunc( cText )

   local nAt, cuc

   do while ( nAt := AT( "\u", cText ) ) > 0
      cuc   := Substr( cText, nAt + 4, 2 )
      cText  := Left( cText, nAt - 1 ) + ;
               Chr( hextonum( cuc ) ) + ;
               SubStr( cText, nAt + 6 )
   enddo

return cText
 
and check
? mytest( "Goyt\u00eda" ) // -> "Goytía"

Re: Unicode to UTF8

Posted: Thu Mar 23, 2023 12:07 pm
by reinaldocrespo
Hello Rao and so good to see you around.

Yes, that will work and thank you so much for clarifying ANSI. No wonder my tests weren't working, I was misguided only looking at UTF. However, I was hoping I would not have to do hb_chr() to translate every single \u. But I like your solution, I'm happy with it and that is exactly how I'm going to solve this problem.

Again, thank you very much.

Reinaldo.