ab.com コミュニティ
https://www.activebasic.com/forum/

URLエンコードとUTF8文字列化
https://www.activebasic.com/forum/viewtopic.php?t=238
ページ 11

作成者:  イグトランス [ 2005年7月27日(水) 19:14 ]
記事の件名:  URLエンコードとUTF8文字列化

URLエンコードをするURLEncode関数と、おまけでShift JISなどのMBCSからUTF8へ変換するToUTF8関数です。

さらにおまけで入力した文字列でGoogle検索をするプログラムを付属させています。
(こいつのためにToUTF8()を作ることになりました)
コード:
#console
TypeDef UINT = DWORD
TypeDef BOOL = Long
/*
Declare Function MultiByteToWideChar Lib "kernel32" (CodePage As UINT, dwFlags As DWORD, pMultiByteStr As BytePtr,
	cchMultiByte As Long, pWideCharStr As *WCHAR, cchWideChar As Long) As Long
*/
Declare Function WideCharToMultiByte Lib "kernel32" (CodePage As UINT, dwFlags As DWORD, pWideByteStr As *WCHAR,
	cchWideByte As Long, pMultiCharStr As BytePtr,cchMultiChar As Long, pDefaultChar As BytePtr, pUsedDefaultChar As *BOOL) As Long 

Function URLEncode(pFrom As BytePtr) As String
	Dim i = 0 As DWord
	While pFrom <> 0
		If IsCharAlphaNumeric(pFrom) Or InStr(1, "'.-*()_", Chr$(pFrom)) Then
			URLEncode = URLEncode + Chr$(pFrom)
		Else
			URLEncode = URLEncode + "%" + Hex$(pFrom)
		End If
		i = i + 1
	Wend
End Function

Function ToUTF8(pFrom As BytePtr) As String
	Dim Length As Long, BufSize As Long
	Length = lstrlen(pFrom)
	BufSize = (Length + 1) * SizeOf(WCHAR)
	Dim pWStr As *WCHAR
	pWStr = malloc(BufSize)
	Dim WStrLength As Long
	WStrLength = MultiByteToWideChar(CP_ACP, 0, pFrom, -1, pWStr, BufSize)
	Dim UTF8StrBufSize As Long
	UTF8StrBufSize = WideCharToMultiByte(CP_ACP, 0, pWStr, WStrLength, NULL, 0, NULL, NULL)
	ToUTF8 = ZeroString(UTF8StrBufSize - 1)
	WideCharToMultiByte(CP_ACP, 0, pWStr, WStrLength, ToUTF8, UTF8StrBufSize, NULL, NULL)
End Function

Dim str As String, strUTF8 As String, strEncoded As String, strURL As String
Input str
strUTF8 = ToUTF8(str)
strEncoded = URLEncode(StrPtr(strUTF8)) 
Print strEncoded
strURL = "http://www.google.co.jp/search?q=" + strEncoded
ShellExecute(0, NULL, StrPtr(strURL), NULL, NULL, SW_SHOW)

ページ 11 全ての表示時間は UTC+09:00 です
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/