「Base64 エンコード」の編集履歴(バックアップ)一覧に戻る

Base64 エンコード - (2010/05/25 (火) 15:43:43) のソース

文字列を入力しBASE64にエンコードしてみるテスト。

#asciiart(blockquote){
#N88BASIC

Function Base64Enc(src As *Byte, dst As *Byte) As Long
	Dim Base64Tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" As *Byte
	Dim i As Long, j As Long, n As Long, t As Long
	Dim p As *Byte

	p = calloc((lstrlen(src)\3+1)*3+1)
	lstrcpy(p, src)
	While p[i]
		t = (p[i]<<16) or (p[i+1]<<8) or (p[i+2]<<0)
		i=i+3
		For n=3 To 0 Step -1
			dst[j] = Base64Tbl[t>>(6*n) And &h3F]
			j=j+1
		Next
	Wend
	
	i = 4- Int(lstrlen(src)*8/6*-1)*-1 mod 4
	If i=4 Then i=0
	For n=1 To i
	dst[j-n] = Base64Tbl[64]
	Next
	dst[j]=0
	free(p)
	Base64Enc =j
End Function


Dim i$ As String,dst[100] As Byte
Print "Base64 エンコード 終わるときはExitと入力"
Do
Input i$
Base64Enc(StrPtr(i$), dst)
Print i$, MakeStr(dst)
Loop While i$<>"Exit"
End
}