Unicode to UTF8

Unicode to UTF8

Postby reinaldocrespo » Thu Mar 23, 2023 12:04 am

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
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: Unicode to UTF8

Postby nageswaragunupudi » Thu Mar 23, 2023 3:42 am

"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 view  RUN
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
nageswaragunupudi
 
Posts: 10646
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Unicode to UTF8

Postby reinaldocrespo » Thu Mar 23, 2023 12:07 pm

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.
User avatar
reinaldocrespo
 
Posts: 979
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 80 guests