Unicode to UTF8

Post Reply
User avatar
reinaldocrespo
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Unicode to UTF8

Post 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.
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Unicode to UTF8

Post 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"
Regards

G. N. Rao.
Hyderabad, India
User avatar
reinaldocrespo
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Unicode to UTF8

Post 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.
Post Reply