怎麼把字串轉換為base

2022-03-18 03:53:15 字數 5922 閱讀 9059

1樓:匿名使用者

建立一個新類,即可呼叫該類的加密和解密方法

option explicit

'base64編碼函式:base64encode

'instr1 編碼前字串

'outstr1 編碼後字串

public function base64encode(instr1 as string) as string

dim minbyte(3) as byte, moutbyte(4) as byte

dim mybyte as byte

dim i as integer, lenarray as integer, j as integer

dim mybarray() as byte

dim outstr1 as string

mybarray() = strconv(instr1, vbfromunicode)

lenarray = ubound(mybarray) + 1

for i = 0 to lenarray step 3

if lenarray - i = 0 then

exit for

end if

if lenarray - i = 2 then

minbyte(0) = mybarray(i)

minbyte(1) = mybarray(i + 1)

base64encodebyte minbyte, moutbyte, 2

elseif lenarray - i = 1 then

minbyte(0) = mybarray(i)

base64encodebyte minbyte, moutbyte, 1

else

minbyte(0) = mybarray(i)

minbyte(1) = mybarray(i + 1)

minbyte(2) = mybarray(i + 2)

base64encodebyte minbyte, moutbyte, 3

end if

for j = 0 to 3

outstr1 = outstr1 & chr(moutbyte(j))

next j

next i

base64encode = outstr1

end function

private sub base64encodebyte(minbyte() as byte, moutbyte() as byte, num as integer)

dim tbyte as byte

dim i as integer

if num = 1 then

minbyte(1) = 0

minbyte(2) = 0

elseif num = 2 then

minbyte(2) = 0

end if

tbyte = minbyte(0) and &hfc

moutbyte(0) = tbyte / 4

moutbyte(1) = tbyte

moutbyte(2) = tbyte

tbyte = (minbyte(2) and &h3f)

moutbyte(3) = tbyte

for i = 0 to 3

if moutbyte(i) >= 0 and moutbyte(i) <= 25 then

moutbyte(i) = moutbyte(i) + asc("a")

elseif moutbyte(i) >= 26 and moutbyte(i) <= 51 then

moutbyte(i) = moutbyte(i) - 26 + asc("a")

elseif moutbyte(i) >= 52 and moutbyte(i) <= 61 then

moutbyte(i) = moutbyte(i) - 52 + asc("0")

elseif moutbyte(i) = 62 then

moutbyte(i) = asc("+")

else

moutbyte(i) = asc("/")

end if

next i

if num = 1 then

moutbyte(2) = asc("=")

moutbyte(3) = asc("=")

elseif num = 2 then

moutbyte(3) = asc("=")

end if

end sub

public function base64decode(instr1 as string) as string

dim minbyte(4) as byte, moutbyte(3) as byte

dim i as integer, lenarray as integer, j as integer

dim mybarray() as byte

dim outstr1 as string

dim tmparray() as byte

mybarray() = strconv(instr1, vbfromunicode)

lenarray = ubound(mybarray)

redim tmparray(((lenarray + 1) / 4) * 3)

j = 0

for i = 0 to lenarray step 4

if lenarray - i = 0 then

exit for

else

minbyte(0) = mybarray(i)

minbyte(1) = mybarray(i + 1)

minbyte(2) = mybarray(i + 2)

minbyte(3) = mybarray(i + 3)

base64decodebyte minbyte, moutbyte, 4

end if

tmparray(j * 3) = moutbyte(0)

tmparray(j * 3 + 1) = moutbyte(1)

tmparray(j * 3 + 2) = moutbyte(2)

j = j + 1

next i

base64decode = binarytostring(tmparray)

end function

private sub base64decodebyte(minbyte() as byte, moutbyte() as byte, bytenum as integer)

dim tbyte as byte

dim i as integer

bytenum = 0

for i = 0 to 3

if minbyte(i) >= asc("a") and minbyte(i) <= asc("z") then

minbyte(i) = minbyte(i) - asc("a")

elseif minbyte(i) >= asc("a") and minbyte(i) <= asc("z") then

minbyte(i) = minbyte(i) - asc("a") + 26

elseif minbyte(i) >= asc("0") and minbyte(i) <= asc("9") then

minbyte(i) = minbyte(i) - asc("0") + 52

elseif minbyte(i) = asc("+") then

minbyte(i) = 62

elseif minbyte(i) = asc("/") then

minbyte(i) = 63

else '"="

bytenum = bytenum + 1

minbyte(i) = 0

end if

next i

'取前六位

'0的六位和1的前兩位

moutbyte(0) = tbyte

'1的後四位和2的前四位

moutbyte(1) = tbyte

moutbyte(2) = tbyte

'2的後兩位和3的六位

end sub

private function binarytostring(byval binarystr as variant) as string '二進位制轉換為字串

dim lnglen as long

dim tmpbin as variant

dim strc as string

dim skipflag as long

dim i as long

skipflag = 0

strc = ""

if not isnull(binarystr) then

lnglen = lenb(binarystr)

for i = 1 to lnglen

if skipflag = 0 then

tmpbin = midb(binarystr, i, 1)

if ascb(tmpbin) > 127 then

strc = strc & chr(ascw(midb(binarystr, i + 1, 1) & tmpbin))

skipflag = 1

else

strc = strc & chr(ascb(tmpbin))

end if

else

skipflag = 0

end if

next

end if

binarytostring = strc

end function

private function stringtobinary(byval varstring as string) as variant '字串轉成二進位制

dim strbin as variant

dim varchar as variant

dim varasc as long

dim varlow, varhigh

dim i as long

strbin = ""

for i = 1 to len(varstring)

varchar = mid(varstring, i, 1)

varasc = asc(varchar)

if varasc < 0 then

varasc = varasc + 65535

end if

if varasc > 255 then

varlow = left(hex(asc(varchar)), 2)

varhigh = right(hex(asc(varchar)), 2)

else

strbin = strbin & chrb(ascb(varchar))

end if

next

stringtobinary = strbin

end function

matlab中如何將字串轉換為命令控制字

eval 這個函式滿足你的要求。如 str1 set text1,string a 1 eval str1 相當於執行了這個語句。請問你這裡的text1是什麼?物件?普通變數?物件的handle?你在引用text1之前,text1是否已經存在?字 符 串 轉 換 abs字串到ascii轉換 dec2...

python怎麼把列表轉換成字串

在python中將列表轉換為字串的方法如下 1 通過桌面快捷方式或搜尋來開啟python idle程式。2 在開啟的介面中輸入一個列表,並將這個列表賦給一個變數,輸入完成後,按鍵盤上的回車鍵。3 換到下一行後,輸入 str 變數名 變數名即是你剛剛把列表賦值給那個變數的名稱,隨後按鍵盤上的回車鍵。4...

C 字元轉數字問題,c 如何將字串轉換為數字?

public string convertstring string value,int frombase,int tobase int intvalue frombase return tobase 各種型的數字之間的轉換,引數 value 傳入的值,frombase 傳入的型別,tobase需要...