TIPEncoder question

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
serge_girard
Posts: 2030
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 378 times
Been thanked: 87 times
Contact:

TIPEncoder question

Post by serge_girard » Tue Jan 09, 2018 12:22 pm

Hello,

I need to retrieve some data from a website with a URL that might contain french accents.

To build my URL I use this:

Code: Select all

cSTR        := 'Brabançonnestraat'
cBHUIS_NR   := '6'
cGEMNM      := 'LEUVEN'

oEncoder := TIPEncoder():New( "url" )
cSTR := oEncoder:Encode( cSTR)
 
url	      := 'http://loc.geopunt.be/geolocation/location?q=' + cSTR + '%20'+ cBHUIS_NR + ',' + ALLTRIM(cGEMNM)

The returned string is:
http://loc.geopunt.be/geolocation/locat ... 206,LEUVEN ==> wrong

instead of:
http://loc.geopunt.be/geolocation/locat ... 206,LEUVEN ==> good

%E7 is Windows-1252 and I need the UTF8 conversion.
How can I force the encoder to convert to UTF8?

See: https://www.w3schools.com/tags/ref_urlencode.asp
Serge

radohabjan
Posts: 95
Joined: Mon Nov 30, 2009 7:17 am
Location: Slovenia
Contact:

Post by radohabjan » Tue Jan 09, 2018 2:33 pm

Hello Serge,

I made my own conversion for Slovenian charachters:

Code: Select all

function utf8
* for check use online converter : http://www.cafewebmaster.com/online_tools/utf8_encode
parameters novtekst
novtekst=strtran(novtekst,"Č","ÄŚ")
novtekst=strtran(novtekst,"Ž","Ĺ˝")
novtekst=strtran(novtekst,"Š","Ĺ ")
novtekst=strtran(novtekst,"Ć","Ć")
novtekst=strtran(novtekst,"Đ","Đ")
novtekst=strtran(novtekst,"č","ÄŤ")
novtekst=strtran(novtekst,"ž","Ĺľ")
novtekst=strtran(novtekst,"š","š")
novtekst=strtran(novtekst,"ć","ć")
novtekst=strtran(novtekst,"đ","Ä‘")
novtekst=strtran(novtekst,"°","°")
novtekst=strtran(novtekst,"§","§")
return alltrim(novtekst)
br Rado

User avatar
serge_girard
Posts: 2030
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 378 times
Been thanked: 87 times
Contact:

Post by serge_girard » Tue Jan 09, 2018 3:54 pm

Hi Rado,

About the same what I am doing now.

Code: Select all

         cSTR      := STRTRAN(cSTR, 'î' ,'%C3%AE')
         cSTR      := STRTRAN(cSTR, 'ï' ,'%C3%AF')
         cSTR      := STRTRAN(cSTR, 'ð' ,'%C3%B0')
         cSTR      := STRTRAN(cSTR, 'ñ' ,'%C3%B1')
         cSTR      := STRTRAN(cSTR, 'ò' ,'%C3%B2')
         cSTR      := STRTRAN(cSTR, 'ó' ,'%C3%B3')
This works but the TIPEncoder should do it also properly !

In Java it goed like this: request = URLEncoder.encode(uri+ queryParam + "&flags=X" + "&gflags=A", "UTF-8");

Serge

edk
Posts: 285
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 76 times
Been thanked: 225 times

Post by edk » Tue Jan 09, 2018 9:23 pm

Hi, Serge it's simple.
First you must convert string to UTF8, then encode it.
I prefer to use the tip_URLEncode() and tip_URLDecode() functions directly, rather than the tipEncoder class:

Code: Select all

#include "hmg.ch"


#include "hblang.ch"
REQUEST HB_CODEPAGE_FRWIN

Procedure main()
SET LANGUAGE TO French
set( _SET_CODEPAGE, "FRWIN" )

cSTR        := 'Brabançonnestraat'
cBHUIS_NR   := '6'
cGEMNM      := 'LEUVEN'

//oEncoder := TIPEncoder():New( "url" )
//cSTR := oEncoder:Encode( cSTR)

cUrlEnc := tip_URLEncode( hb_StrToUTF8( cStr ) )
 
url := 'http://loc.geopunt.be/geolocation/location?q=' + cUrlEnc + ' %20' + cBHUIS_NR + ',' + ALLTRIM(cGEMNM)

msgbox(url)

return
Bez tytułu.png
Bez tytułu.png (3.04 KiB) Viewed 342 times

User avatar
serge_girard
Posts: 2030
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 378 times
Been thanked: 87 times
Contact:

Post by serge_girard » Wed Jan 10, 2018 6:28 am

Of course! Big thank you !

Serge

Post Reply