FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Unicode to UTF8
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Unicode to UTF8
Posted: 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.

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Unicode to UTF8
Posted: Thu Mar 23, 2023 03: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 (fw): Select all Collapse
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
Posts: 990
Joined: Thu Nov 17, 2005 05:49 PM
Re: Unicode to UTF8
Posted: 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.

Continue the discussion