Base64 encoding and decoding functions

Source: Internet
Author: User
Tags base64 exit character set chr functions
Code | Function This is my own rewrite after reading a few base64 encoding and decoding functions.
Because, in the Chinese operating system of VBScript, using the Unicode character set, so
Many base64 encoding and decoding functions are theoretically correct, but they do not actually work!

I added a few Unicode and ANSI code conversion functions, now posted, please teach!


File name base64test.asp

<%

Sbase_64_characters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
Sbase_64_characters = Strunicode2ansi (sbase_64_characters)

Function Strunicodelen (ascontents)
' Calculates the length of the ANSI encoding of a Unicode string
Ascontents1= "a" &ascontents
Len1=len (ASCONTENTS1)
K=0
For I=1 to Len1
ASC1=ASC (Mid (ascontents1,i,1))
If Asc1<0 then ASC1=65536+ASC1
If asc1>255 Then
K=k+2
Else
K=k+1
End If
Next
Strunicodelen=k-1
End Function

Function Strunicode2ansi (ascontents)
' Converts a Unicode-encoded string into an ANSI-encoded string
Strunicode2ansi= ""
Len1=len (ascontents)
For I=1 to Len1
Varchar=mid (ascontents,i,1)
VARASC=ASC (varchar)
If Varasc<0 then varasc=varasc+65536
If varasc>255 Then
Varhex=hex (VARASC)
Varlow=left (varhex,2)
Varhigh=right (varhex,2)
Strunicode2ansi=strunicode2ansi & ChrB ("&h" & Varlow) & ChrB ("&h" & Varhigh)
Else
Strunicode2ansi=strunicode2ansi & ChrB (VARASC)
End If
Next
End Function

Function Stransi2unicode (ascontents)
' Converts an ANSI-encoded string into a Unicode-encoded string
Stransi2unicode = ""
Len1=lenb (ascontents)
If Len1=0 then Exit function
For I=1 to Len1
VARCHAR=MIDB (ascontents,i,1)
VARASC=ASCB (varchar)
If VARASC > 127 Then
Stransi2unicode = Stransi2unicode & Chr (AscW (MidB (ascontents,i+1,1) & varchar))
I=i+1
Else
Stransi2unicode = Stransi2unicode & Chr (VARASC)
End If
Next
End Function

Function Base64Encode (ascontents)
' BASE64 encoding an ANSI-encoded string
' Ascontents should be an ANSI-encoded string (the binary string can also)
Dim lnposition
Dim Lsresult
Dim Char1
Dim CHAR2
Dim CHAR3
Dim CHAR4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsgroupbinary
Dim LSGROUP64
Dim M4,len1,len2

Len1=lenb (ascontents)
If Len1<1 Then
Base64encode= ""
Exit Function
End If

M3=len1 Mod 3
If M3 > 0 Then ascontents = ascontents & String (3-m3, ChrB (0))
' The number of complements is for ease of calculation.

IF m3 > 0 THEN
len1=len1+ (3-M3)
Len2=len1-3
Else
Len2=len1
End If

Lsresult = ""

For lnposition = 1 to Len2 step 3
LSGROUP64 = ""
Lsgroupbinary = Midb (ascontents, lnposition, 3)

Byte1 = ASCB (Midb (lsgroupbinary, 1, 1)): SaveBits1 = Byte1 and 3
Byte2 = ASCB (Midb (Lsgroupbinary, 2, 1)): SaveBits2 = Byte2 and 15
Byte3 = ASCB (Midb (Lsgroupbinary, 3, 1))

Char1



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.