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


UrlEscape %ASCIIに変換可能な安全ではない文字をエスケープシーケンスに変換

UrlUnescape エスケープシーケンスを普通の文字に変換

 

 

キャラクタとエスケープシーケンス

    ^    %5E
    &    %26
    `    %60
    {    %7B
    }    %7D
    |    %7C
    ]    %5D
    [    %5B
    "    %22
    <    %3C
    >    %3E
    \    %5C

 

'================================================================
'= %ASCIIに変換可能な安全ではない文字をエスケープシーケンスに変換
'=    (UrlEscape.bas)
'================================================================
#include "Windows.bi"

#define MAX_PATH 260
#define ERROR_SUCCESS  0

#define URL_ESCAPE_SEGMENT_ONLY &H2000
#define URL_ESCAPE_PERCENT &H1000
#define URL_UNESCAPE_INPLACE &H100000

#define URL_INTERNAL_PATH &H800000
#define URL_DONT_ESCAPE_EXTRA_INFO &H2000000
#define URL_ESCAPE_SPACES_ONLY &H4000000
#define URL_DONT_SIMPLIFY &H8000000

' %ASCIIに変換可能な安全ではない文字をエスケープシーケンスに変換
Declare Function Api_UrlEscape& Lib "shlwapi" Alias "UrlEscapeA" (ByVal pszURL$, ByVal pszEscaped$, pcchEscaped&, ByVal dwFlags&)

' エスケープシーケンスを普通の文字に変換
Declare Function Api_UrlUnescape& Lib "shlwapi" Alias "UrlUnescapeA" (ByVal pszURL$, ByVal pszUnescaped$, pcchUnescaped&, ByVal dwFlags&)

Var Shared Text(1) As Object
Var Shared Edit(1) As Object
Var Shared Button(1) As Object

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

'================================================================
'=
'================================================================
Declare Function EncodeUrl(sUrl As String) As String
Function EncodeUrl(sUrl As String) As String
    Var buff As String
    Var dwSize As Long
    Var dwFlags As Long
   
    If Len(sUrl) > 0 Then
        buff = Space$(MAX_PATH)
        dwSize = Len(buff)
        dwFlags = URL_DONT_SIMPLIFY
        If Api_UrlEscape(sUrl, buff, dwSize, dwFlags) = ERROR_SUCCESS Then
            EncodeUrl = Left$(buff, dwSize)
        End If
    End If
End Function

'================================================================
'=
'================================================================
Declare Function DecodeUrl(sUrl As String) As String
Function DecodeUrl(sUrl As String) As String
    Var buff As String
    Var dwSize As Long
    Var dwFlags As Long
   
    If Len(sUrl) > 0 Then
        buff = Space$(MAX_PATH)
        dwSize = Len(buff)
        dwFlags = URL_DONT_SIMPLIFY
        If Api_UrlUnescape(sUrl, buff, dwSize, dwFlags) = ERROR_SUCCESS Then
            DecodeUrl = Left$(buff, dwSize)
        End If        'UrlUnescape
    End If            'Len(sUrl)
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
   Edit(0).SetWindowText "http://apinet.co.jp/fbapi code Lib/net code/<index address>.htm"
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var sUrl As String
    Var buff As String

    sUrl = Edit(0).GetWindowText()
    buff = EncodeUrl(sUrl)
   
    Text(1).SetWindowText "エンコード"
    EDit(1).SetWindowText buff
End Sub

'================================================================
'=
'================================================================
Declare Sub Button2_on edecl ()
Sub Button2_on()
    Var sUrl As String
    Var buff As String

    sUrl = Edit(1).GetWindowText()
    buff = DecodeUrl(sUrl)
   
    Text(1).SetWindowText "デコード"
    Edit(1).SetWindowText buff
End Sub

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