This is what I use.
It simply replaces all %?? hex values with the correct char
and not just %20 with char$(32).

FUNCTION urlencode(s AS String) AS String
 
 DIM p AS Integer   'position in string for %
 DIM h1 AS Integer  'the hex 2 digits, separately
 DIM h2 AS Integer 

 REPEAT
   p = Instr(s,"%")   'is there a % in the string
   IF p > 0 THEN
     h1 = CInt(Mid(s, p + 1, 1))      'isolate first of the 2 hex chars 
     h2 = CInt(Mid(s, p + 2, 1)) * 16 'isolate second of the 2 hex chars
     s = Left$(s, p - 1) & Chr$(h1 + h2) & Mid$(s, p + 3) 'recombine result
   ENDIF
 UNTIL p=0

 RETURN s
END


---------- Original Message -----------
From: werner 007 <ad...@micro-source.ch>
To: gambas-user@lists.sourceforge.net
Sent: Tue, 30 Dec 2008 16:44:05 -0800 (PST)
Subject: Re: [Gambas-user] URL encoding "%20"

> Rob Kudla wrote:
> > 
> > On Tuesday 30 December 2008 08:03, werner 007 wrote:
> >> Ok, i got the answer by my self.
> >> The chars of not printable and non-ascii are represented as Hex.
> >> To convert it back:
> > 
> > That'll work, but it'll also run Replace() 160 times each time you do a 
> > conversion.  I'd do something like this:
> > 
> > function urlencode(strin as string) as string
> >     dim strout as string
> >     dim i as integer
> >     dim a as integer
> >     for i = 1 to len(strin)
> >             a = asc(mid(strin, i, 1))
> >             if a < 33 or a > 126 then
> >                strout = strout & "%" & Hex$(a, 2)
> >             else
> >                strout = strout & mid(strin, i, 1)
> >             endif
> >     next
> >     return strout
> > end
> > 
> > Rob
> > 
> >
> 
> Hi Rob
> 
> First when i read your solution i was thinking "damn, i was lazy to make a
> good job". 
> But then, we are not looking for single chars but for three at once. 
> So i modified your idea to
> 
> FUNCTION urlencode(strin AS String) AS String
>   DIM strout AS String
>   DIM i AS Integer
>   DIM a AS Integer
>   IF InStr(strin, "%") THEN ' we can safe cpu
>   FOR i = 1 TO Len(strin)
>       IF Mid(strin, i, 1) = "%" THEN ' we can proceed next 2 digits
>         IF IsNumber(Val(Mid(strin, i + 1, 2))) THEN 
>           strout &= Chr((CInt(Mid(strin, i + 1, 2)) / 10) * 16) 'convert hex
> to decimal
>           i = i + 2
>         ELSE
>          strout = strout & Mid(strin, i, 1)
>         END IF
>       ELSE 
>         strout = strout & Mid(strin, i, 1)
>       END IF
>   NEXT
>   END IF
>   RETURN strout
> END
> 
> Werner(007)
> 
> -- 
> View this message in context:
http://www.nabble.com/URL-encoding-%22-20%22-tp21216641p21225597.html
> Sent from the gambas-user mailing list archive at Nabble.com.
> 
> ------------------------------------------------------------------------------
> _______________________________________________
> Gambas-user mailing list
> Gambas-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------


------------------------------------------------------------------------------
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to