文字をエスケープシーケンスに変換(U)          <TOP>


URL文字列をエスケープシーケンスに変換(エンコード)・戻す(デコード)

 

 
'================================================================
'= 文字をエスケープシーケンスに変換(U)
'=    (URLEncodeDecode.bas)
'================================================================
#include "Windows.bi"

Var Shared Text(2) As Object
Var Shared Edit1 As Object

For i = 0 To 2
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 12
Next
Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 12

'================================================================
'=
'================================================================
Declare Function ChangeToHex(Number As Long, Numlen As Integer) As String
Function ChangeToHex(Number As Long, Numlen As Integer) As String
    Var cHex As String
    
    cHex = hex$(Number)

    If Len(cHex) < Numlen Then
        cHex = String$(Numlen - Len(cHex), "0") & cHex
    End If
 
    If Not UpperCase Then
        ChangeToHex = lcase$(cHex)
    Else
        ChangeToHex = cHex
    End If
 
End Function

'================================================================
'= URLエンコード
'================================================================
Declare Function URLEncode(sInput As String) As String
Function URLEncode(sInput As String) As String
    Var sRet As String
    Var lChr As Long
    
    Do While Len(sInput) > 0
        lChr = Asc(sInput)
        sInput = Right$(sInput, Len(sInput) - 1)
        If (lChr >= Asc("a") And lChr <= Asc("z")) Or (lChr >= Asc("A") And lChr <= asc("Z")) Or (lChr >= Asc("0") and lChr <= Asc("9")) Then
            sRet = sRet & Chr$(lChr)
        Else If lChr = Asc(" ") Then
            sRet = sRet & "+"
        Else
            sRet = sRet & "%" & ChangeToHex(lChr, 2)
        End If
    Loop
    
    URlencode = sRet
End Function

'================================================================
'= URLデコード
'================================================================
Declare Function URLDecode(sInput As String) As String
Function URLDecode(sInput As String) As String
    Var sRet As String
    Var lChr As Long
    Var sChr As String
    Var i As Long
    
    For i = 1 To Len(sInput)
        sChr = Mid$(sInput, i, 1)
        If sChr = "+" Then
            sRet = sRet & " "
        Else If sChr = "%" Then
            sRet = sRet & Chr$(Val("&H" & Mid$(sInput, i + 1, 1) & Mid$(sInput, i + 2, 1)))
            i = i + 2
        Else
            sRet = sRet & sChr
        End If
    Next
    
    URLDecode = sRet
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Text(1).SetWindowText "エンコード"
    Text(2).SetWindowText URLEncode(Edit1.GetWindowText)
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Text(1).SetWindowText "デコード"
    Text(2).SetWindowText URLDecode(Edit1.GetWindowText)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End