decript string...

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

decript string...

Post by sal21 »

Based this string how to decript????

data=%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Epesaro%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A"

User avatar
HansV
Administrator
Posts: 79370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: decript string...

Post by HansV »

You can replace the hexadecimal characters using the function

Code: Select all

Function ReplaceChars(ByVal s As String) As String
    Dim i As Long
    Do
        i = InStrRev(s, "%")
        If i = 0 Then Exit Do
        s = Left(s, i - 1) & Chr("&H" & Mid(s, i + 1, 2)) & Mid(s, i + 3)
    Loop
    ReplaceChars = s
End Function
but what you do with the result is up to you...
Best wishes,
Hans

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: decript string...

Post by snb »

Code: Select all

Sub M_snb()
  c00 = "%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Epesaro%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A"

  sn = Split(c00, "%")
  
  For j = 1 To UBound(sn)
    sn(j) = Mid(sn(j), 3)
  Next
  
  MsgBox Application.Trim(Join(sn))
End Sub

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

HansV wrote:
14 Sep 2023, 16:56
You can replace the hexadecimal characters using the function

Code: Select all

Function ReplaceChars(ByVal s As String) As String
    Dim i As Long
    Do
        i = InStrRev(s, "%")
        If i = 0 Then Exit Do
        s = Left(s, i - 1) & Chr("&H" & Mid(s, i + 1, 2)) & Mid(s, i + 3)
    Loop
    ReplaceChars = s
End Function
but what you do with the result is up to you...
really my code is:

Code: Select all


Sub TEST_JSON_PARSE()

'verificare
'https://data.opendatasoft.com/explore/dataset/rifter_civici_pt%40bologna/export/

    'myurl = "https://overpass-api.de/api/interpreter?data=%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Enapoli%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A"

    
    Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
    
    myurl = "https://overpass-api.de/api/interpreter?data=%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Epesaro%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A"
    'oppure senza autorizzazione
    'https://samurai016.github.io/Comuni-ITA/#tag/comuni
    'myurl = "https://axqvoqvbfjpaamphztgd.functions.supabase.co/comuni"
    
    'ReplaceChars myurl
    
    With xmlhttp
        
        .Open "GET", myurl, False
        '.setRequestHeader "Authorization", "Bearer 6357ac75baecf33196160c21"
        .send
        Debug.Print .responseText
        
        Dim Handle As Integer
        Handle = FreeFile
        Open "C:\Lavori_Vb6\REST_API_OPENAPI\JSON_FILE\comuni.txt" For Output As #Handle
        Print #Handle, .responseText
        Close #Handle
        
        Call LEGGI_JSON_FILE
        
        'ReadFile "C:\Lavori_Vb6\REST_API_OPENAPI\JSON_FILE\PROVINCE.txt"
        
    End With
    
End Sub
whe i pass the myurl to the api rest in debug.print t .responseText have a correct result in csv.

but i need to replace the bologna city with other... similar napoli

User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: decript string...

Post by SpeakEasy »

Pretty sure there is an API call that can do this fairly simply, UrlUnescapeA. Sadly I'm currently on holiday, and nowhere near a PC so can't provide an example.

You could also leverage javascript's UnEscape function
Last edited by SpeakEasy on 15 Sep 2023, 21:52, edited 1 time in total.

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

SpeakEasy wrote:
15 Sep 2023, 21:43
Pretty sure there is an API call that can do this fairly simply, UrlUnescapeA. Sadly I'm currently on holiday, and nowhere near a PC so can't provide an example.
Ok

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: decript string...

Post by snb »


User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: decript string...

Post by SpeakEasy »

Ok, so here's a possible API solution...

Code: Select all

Option Explicit

Private Declare Function UrlUnescape Lib "shlwapi" Alias "UrlUnescapeA" (ByVal pszURL As String, ByVal pszUnescaped As String, pcchUnescaped As Long, ByVal dwFlags As Long) As Long
Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000

Public Function Decode(strDecodeMe As String) As String
    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then Decode = Left(strResult, mychars)
End Function

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

SpeakEasy wrote:
23 Sep 2023, 13:06
Ok, so here's a possible API solution...

Code: Select all

Option Explicit

Private Declare Function UrlUnescape Lib "shlwapi" Alias "UrlUnescapeA" (ByVal pszURL As String, ByVal pszUnescaped As String, pcchUnescaped As Long, ByVal dwFlags As Long) As Long
Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000

Public Function Decode(strDecodeMe As String) As String
    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then Decode = Left(strResult, mychars)
End Function
tks.
but i cannot test now.

User avatar
HansV
Administrator
Posts: 79370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: decript string...

Post by HansV »

You have asked other questions since the above post. Have you found time to test SpeakEasy's suggestion yet?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

No. Sorry. Prob. In family

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

SpeakEasy wrote:
23 Sep 2023, 13:06
Ok, so here's a possible API solution...

Code: Select all

Option Explicit

Private Declare Function UrlUnescape Lib "shlwapi" Alias "UrlUnescapeA" (ByVal pszURL As String, ByVal pszUnescaped As String, pcchUnescaped As Long, ByVal dwFlags As Long) As Long
Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000

Public Function Decode(strDecodeMe As String) As String
    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then Decode = Left(strResult, mychars)
End Function
bro, is this code correct?

Code: Select all

Option Explicit
Private Declare Function UrlUnescape Lib "shlwapi" Alias "UrlUnescapeA" (ByVal pszURL As String, ByVal pszUnescaped As String, pcchUnescaped As Long, ByVal dwFlags As Long) As Long
Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000
Public Function Decode(strDecodeMe As String) As String

    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then Decode = Left(strResult, mychars)
    
    'Debug.Print strResult
    
End Function
Sub uno()

Dim strDecodeMe As String

strDecodeMe = "https://overpass-api.de/api/interpreter?data=%0A%5Bout%3Ajson%5D%5Btimeout%3A925%5D%3B%0A%28%0A%20%20area%5B%22name%22%3D%22Cesena%22%5D-%3E.boundaryarea%3B%0A%20%20node%5B%22addr%3Acity%22%5D%28area.boundaryarea%29%3B%0A%20%20way%5B%22highway%22%5D%28area.boundaryarea%29%3B%0A%20%0A%29%3B%0A%2F%2F%20print%20results%0Aout%20body%3B%0A%3E%3B%0Aout%20skel%20qt%3B"
Decode strDecodeMe

End Sub

i dont see in other case the result of decoded string...

User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: decript string...

Post by SpeakEasy »

You've changed your requirement slightly since your original post., and now you do want 'EXTRA_INFO'

So the function can be modified to:

Code: Select all

Public Function Decode(strDecodeMe As String) As String
    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, 0&) = 0 Then Decode = Left(strResult, mychars)
End Function
and an example calling it using your latest current example string:

Code: Select all

Public Sub OneOhOne()
    Dim strDecodeMe As String
    strDecodeMe = "https://overpass-api.de/api/interpreter?data=%0A%5Bout%3Ajson%5D%5Btimeout%3A925%5D%3B%0A%28%0A%20%20area%5B%22name%22%3D%22Cesena%22%5D-%3E.boundaryarea%3B%0A%20%20node%5B%22addr%3Acity%22%5D%28area.boundaryarea%29%3B%0A%20%20way%5B%22highway%22%5D%28area.boundaryarea%29%3B%0A%20%0A%29%3B%0A%2F%2F%20print%20results%0Aout%20body%3B%0A%3E%3B%0Aout%20skel%20qt%3B"
    MsgBox Decode(strDecodeMe)
End Sub

User avatar
sal21
PlatinumLounger
Posts: 4481
Joined: 26 Apr 2010, 17:36

Re: decript string...

Post by sal21 »

SpeakEasy wrote:
01 Oct 2023, 16:41
You've changed your requirement slightly since your original post., and now you do want 'EXTRA_INFO'

So the function can be modified to:

Code: Select all

Public Function Decode(strDecodeMe As String) As String
    Dim strResult As String
    Dim mychars As Long
    
    strResult = Space(Len(strDecodeMe))
    mychars = Len(strDecodeMe)
    If UrlUnescape(strDecodeMe, strResult, mychars, 0&) = 0 Then Decode = Left(strResult, mychars)
End Function
and an example calling it using your latest current example string:

Code: Select all

Public Sub OneOhOne()
    Dim strDecodeMe As String
    strDecodeMe = "https://overpass-api.de/api/interpreter?data=%0A%5Bout%3Ajson%5D%5Btimeout%3A925%5D%3B%0A%28%0A%20%20area%5B%22name%22%3D%22Cesena%22%5D-%3E.boundaryarea%3B%0A%20%20node%5B%22addr%3Acity%22%5D%28area.boundaryarea%29%3B%0A%20%20way%5B%22highway%22%5D%28area.boundaryarea%29%3B%0A%20%0A%29%3B%0A%2F%2F%20print%20results%0Aout%20body%3B%0A%3E%3B%0Aout%20skel%20qt%3B"
    MsgBox Decode(strDecodeMe)
End Sub
NO COMMENT!!!!
EXACTLY WHAT I NEEDED

Tks for patience.

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: decript string...

Post by snb »

Alternative ?

Code: Select all

Sub M_snb()
    c00 = "https://overpass-api.de/api/interpreter?data=%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Enapoli%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A"
    sn = Split(c00, "%")
    For j = 1 To UBound(sn)
       sn(j) = Chr(Application.Hex2Dec(Left(sn(j), 2))) & Mid(sn(j), 3)
    Next
    MsgBox Join(sn, "")
End Sub

User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: decript string...

Post by SpeakEasy »

Yep, works, but dramatically slower than the API solution - but it seems unlikely that the OP will be dealing with the thousands of URLs that might make that an important differentiator!

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: decript string...

Post by snb »

Maybe faster (?):

Code: Select all

Sub M_tst()
  c00 = "123456789ABCDEFG"

  sn = Split("https://overpass-api.de/api/interpreter?data=%5Bout%3Acsv%28comune%2C%20provincia%2C%20name%2C%20highway%2C%20%3A%3Alat%2C%3A%3Alon%3Btrue%3B%22%3B%22%29%5D%5Btimeout%3A600%5D%3B%0A%2F%2Fprovincia%20da%20cui%20estrarre%20i%20dati%0Aarea%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D6%5D%5Bname~%22%5Enapoli%22%2Ci%5D-%3E.searchArea%3B%0Arelation%5Bboundary%3Dadministrative%5D%5B%22admin_level%22%3D8%5D%28area.searchArea%29%3B%0Aforeach%20%28%0A%20%20map_to_area-%3E.comune%3B%0A%20%20make%20stat%20comune%3Dcomune.set%28t%5B%22name%22%5D%29%2Cprovincia%3DsearchArea.set%28t%5B%22short_name%22%5D%29%3B%0A%20%20out%3B%0A%20%20way%5Bhighway~%22residential%7Cunclassified%7Ctertiary%7Csecondary%7Cprimary%22%5D%5Bname%5D%28area.comune%29%3B%0A%20%20out%20center%3B%0A%29%3B%0A", "%")
    
  For j = 1 To UBound(sn)
    c01 = Left(sn(j), 2)
    sn(j) = Chr(16 * InStr(c00, Left(c01, 1)) + InStr(c00, Right(c01, 1))) & Mid(sn(j), 3)
  Next

  MsgBox Join(sn, "")
End Sub

User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: decript string...

Post by SpeakEasy »

Yep, that's faster! But, when sticking this into a timing rig I noticed that the original - M_snb - uses Excel's Hex2Dec function. That a) really slows things down, and b) Sal21 uses VB6, which doesn't have Hex2Dec. Simply replacing that with CLng("&h" & Left(sn(j), 2)) makes M_snb fractionally faster than M_tst

But as I say, unless the OP is handling thousands of urls in one go, any of these are suitable solutions