VBScript で、コード内の文字列を UTF-8 に変換してから、一旦全てパーセントエンコードし、目的の仕様に沿って一部の文字を元に戻しています。 ※ rfc3986 は、PHP 5.3.x における rawurlencode です。 関連する記事 PHP / JavaScript / ASP / PowerShell / python / Framework(バージョン別) : 処理別の urlencode の結果の違い VBScript : Twitter API を呼び出して投稿する
Set Stream = Wscript.CreateObject("ADODB.Stream")
Set Stream2 = Wscript.CreateObject("ADODB.Stream")
Set StreamBin = Wscript.CreateObject("ADODB.Stream")
strData = "status=" & rfc3986_convert(URLEncode(strText))
' ********************************************
' SHIFT_JIS を UTF-8 に変換して URLエンコード
' ※ 全ての文字をパーセントエンコーディングします
' ********************************************
Function URLEncode(str)
Stream.Open
Stream.Charset = "shift_jis"
' shift_jis で入力文字を書き込む
Stream.WriteText str
' コピーの為にデータポインタを先頭にセット
Stream.Position = 0
Stream2.Open
Stream2.Charset = "utf-8"
' shift_jis を utf-8 に変換
Stream.CopyTo Stream2
Stream.Close
' コピーの為にデータポインタを先頭にセット
Stream2.Position = 0
' バイナリで開く
StreamBin.Open
StreamBin.Type = 1
' テキストをバイナリに変換
Stream2.CopyTo StreamBin
Stream2.Close
' 読み込みの為にデータポインタを先頭にセット
StreamBin.Position = 0
Buffer = ""
' BOMを取り去る
StreamBin.Read(3)
Do while not StreamBin.EOS
LineBuffer = StreamBin.Read(16)
For i = 1 to LenB( LineBuffer )
CWork = MidB(LineBuffer,i,1)
Cwork = AscB(Cwork)
Cwork = Hex(Cwork)
Cwork = Ucase(Cwork)
if Len(Cwork) = 1 then
Buffer = Buffer & "%0" & Cwork
else
Buffer = Buffer & "%" & Cwork
end if
Next
Loop
StreamBin.Close
URLEncode = Buffer
End Function
' ********************************************
' 仕様を明確にする為に単純変換
' ********************************************
Function rfc3986_convert(str)
Dim strResult,I,strWork
strResult = str
strResult = Replace(strResult,"%2D", "-")
strResult = Replace(strResult,"%2E", ".")
' 0〜9
For I = &H30 to &H39
strWork = Hex(I)
strWork = "%" & Ucase(strWork)
strResult = Replace(strResult,strWork, Chr(I))
Next
' A〜Z
For I = &H41 to &H5A
strWork = Hex(I)
strWork = "%" & Ucase(strWork)
strResult = Replace(strResult,strWork, Chr(I))
Next
strResult = Replace(strResult,"%5F", "_")
' a〜z
For I = &H61 to &H7A
strWork = Hex(I)
strWork = "%" & Ucase(strWork)
strResult = Replace(strResult,strWork, Chr(I))
Next
strResult = Replace(strResult,"%7E", "~")
rfc3986_convert = strResult
End Function
以下はいろいろな処理系の URLEncode か、またはそれに相当する変換の一部分です
| 1 | PHP : urlencode |
| 2 | PHP : rawurlencode : 5.2.x |
| 3 | PHP : rawurlencode : 5.3.x |
| 4 | JavaScript : encodeURI |
| 5 | JavaScript : encodeURIComponent |
| 6 | ASP : Server.URLEncode ( Server.URLEncode(chr(I))で変換できない文字があります ) |
| 7 | PowerShell : [System.Web.HttpUtility]::UrlEncode |
| 8 | python 2.6.2 : urllib.quote |
| 9 | js2 と同じ : VS2010 Uri.EscapeDataString |
| 10 | php3 と同じ : VS2012 Uri.EscapeDataString |
| 11 | ps の ' が %27 : VS2010 HttpUtility.UrlEncode |
| php1 | php2 | php3 | js1 | js2 | asp | ps | py | fw4 | fw4.5 | fw | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ||
| 0 | %00 | %00 | %00 | %00 | %00 | %00 | %00 | %00 | %00 | %00 | ||
| 32 | + | %20 | %20 | %20 | %20 | + | + | %20 | %20 | %20 | + | |
| 33 | 【!】 | %21 | %21 | %21 | ! | ! | %21 | ! | %21 | ! | %21 | ! |
| 34 | 【"】 | %22 | %22 | %22 | %22 | %22 | %22 | %22 | %22 | %22 | %22 | %22 |
| 35 | 【#】 | %23 | %23 | %23 | # | %23 | %23 | %23 | %23 | %23 | %23 | %23 |
| 36 | 【$】 | %24 | %24 | %24 | $ | %24 | %24 | %24 | %24 | %24 | %24 | %24 |
| 37 | 【%】 | %25 | %25 | %25 | %25 | %25 | %25 | %25 | %25 | %25 | %25 | %25 |
| 38 | 【&】 | %26 | %26 | %26 | & | %26 | %26 | %26 | %26 | %26 | %26 | %26 |
| 39 | 【'】 | %27 | %27 | %27 | ' | ' | %27 | ' | %27 | ' | %27 | %27 |
| 40 | 【(】 | %28 | %28 | %28 | ( | ( | %28 | ( | %28 | ( | %28 | ( |
| 41 | 【)】 | %29 | %29 | %29 | ) | ) | %29 | ) | %29 | ) | %29 | ) |
| 42 | 【*】 | %2A | %2A | %2A | * | * | %2A | * | %2A | * | %2A | * |
| 43 | 【+】 | %2B | %2B | %2B | + | %2B | %2B | %2B | %2B | %2B | %2B | %2B |
| 44 | 【,】 | %2C | %2C | %2C | , | %2C | %2C | %2C | %2C | %2C | %2C | %2C |
| 45 | 【-】 | - | - | - | - | - | %2D | - | - | - | - | - |
| 46 | 【.】 | . | . | . | . | . | %2E | . | . | . | . | . |
| 47 | 【/】 | %2F | %2F | %2F | / | %2F | %2F | %2F | / | %2F | %2F | %2F |
VBScriptドキュメント
|
|
【VBS + インターネットの最新記事】
- VBScript / JScript: Windows標準のオブジェクト( CDO.Message ) と ロリポップメールを使ってメール送信
- VBScriptの関数定義をWEB上に置いて、Msxml2.ServerXMLHTTP で読みだして PCで使用する
- VBScript : Seesaa ブログのエクスポートを呼び出して全てをバックアップするスクリプト
- WSF : VBScript の 関数定義を WEB 上に置いて PC で使用する
- VBScript を使って HTTPプロトコルで PHP へファイルをアップロードする方法
- VBscript(または JScript) で簡単にバイナリファイルをアップロードする
- VBScript で Seesaaブログへ禁止ワード一括登録( アップロード )
- VBScript で半角カナから全角カナへ変換するのに、php の mb_convert_kana を呼び出す
- VBS : Textt サービスに書き込んだテキストを PC にダウンロード(配布)する
- IE限定。信頼するサイトのページをボタンから印刷プレビュー表示する
【VBScript関連のカテゴリ】






