Thursday, January 8, 2009

URL encode decode

Function URLDecode(str)
str = Replace(str, "+", " ")
For i = 1 To Len(str)
sT = Mid(str, i, 1)
If sT = "%" Then
If i+2 < Len(str) Then
sR = sR & _
Chr(CLng("&H" & Mid(str, i+1, 2)))
i = i+2
End If
Else
sR = sR & sT
End If
Next
URLDecode = sR
End Function


Function charToHex( str )
strResult = ""
leng = len(str)
For i = 1 To leng Step 1
strResult = strResult & hex( asc( mid( str, i, 1) ) )
Next
charToHex = strResult
End Function

Function charToDecimal( str )
strResult = ""
leng = len(str)
For i = 1 To leng Step 1
strResult = strResult & asc( mid( str, i, 1) )
Next
charToDecimal = strResult
end Function

Function trim0x(str)
strTmp = mid(str, 1, 2)
if strTmp = "0x" or strTmp = "0X" then
str = mid( str, 3)
end if
trim0x = str
End Function



Function decimalToChar( strDec )
strResult = ""
count = -1
leng = len(strDec)/2
For i = 1 To leng Step 1
count = count + 2
strResult = strResult & Chr( hexToDecimal( Mid( strDec, count, 2 ) ) )
next
decimalToChar = strResult
End Function

Function hexToDecimal(strHex)
dim lngResult
dim intIndex
dim strDigit
dim intDigit
dim intValue

lngResult = 0
for intIndex = len(strHex) to 1 step -1
strDigit = mid(strHex, intIndex, 1)
intDigit = instr("0123456789ABCDEF", ucase(strDigit))-1
if intDigit >= 0 then
intValue = intDigit * (16 ^ (len(strHex)-intIndex))
lngResult = lngResult + intValue
else
lngResult = 0
intIndex = 0 ' stop the loop
end if
next

hexToDecimal = lngResult
End Function

No comments: