[Gambas-user] URL encoding "%20"

werner 007 admin at ...1080...
Wed Dec 31 01:44:05 CET 2008



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.





More information about the User mailing list