请教高手:UUencode/decode中文问题
我用下面函数来编码/解码邮件附件,为什么一遇到中文,文件就不能还原??????
Public Function UUEncodeFile(strFilePath As String) As String
Dim intFile As Integer 'file handler
Dim intTempFile As Integer 'temp file
Dim lFileSize As Long 'size of the file
Dim strFileName As String 'name of the file
Dim strFileData As String 'file data chunk
Dim lEncodedLines As Long 'number of encoded lines
Dim strTempLine As String 'temporary string
Dim i As Long 'loop counter
Dim j As Integer 'loop counter
Dim strResult As String
'
'Get file name
strFileName = Mid$(strFilePath, InStrRev(strFilePath, "\") + 1)
'
'Insert first marker: "begin 664 ..."
strResult = "begin 664 " + strFileName + vbCrLf
'
'Get file size
lFileSize = FileLen(strFilePath)
lEncodedLines = lFileSize \ 45 + 1
'
'Prepare buffer to retrieve data from
'the file by 45 symbols chunks
strFileData = Space(45)
'
intFile = FreeFile
'
Open strFilePath For Binary As intFile
For i = 1 To lEncodedLines
'Read file data by 45-bytes cnunks
'
If i = lEncodedLines Then
'Last line of encoded data often is not
'equal to 45, therefore we need to change
'size of the buffer
strFileData = Space(lFileSize Mod 45)
End If
'Retrieve data chunk from file to the buffer
Get intFile, , strFileData
'Add first symbol to encoded string that informs
'about quantity of symbols in encoded string.
'More often "M" symbol is used.
strTempLine = Chr(Len(strFileData) + 32)
'
If i = lEncodedLines And (Len(strFileData) Mod 3) Then
'If the last line is processed and length of
'source data is not a number divisible by 3, add one or two
'blankspace symbols
strFileData = strFileData + Space(3 - (Len(strFileData) Mod 3))
End If
For j = 1 To Len(strFileData) Step 3
'Breake each 3 (8-bits) bytes to 4 (6-bits) bytes
'
'1 byte
strTempLine = strTempLine + Chr(Asc(Mid(strFileData, j, 1)) \ 4 + 32)
'2 byte
strTempLine = strTempLine + Chr((Asc(Mid(strFileData, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strFileData, j + 1, 1)) \ 16 + 32)
'3 byte
strTempLine = strTempLine + Chr((Asc(Mid(strFileData, j + 1, 1)) Mod 16) * 4 _
+ Asc(Mid(strFileData, j + 2, 1)) \ 64 + 32)
'4 byte
strTempLine = strTempLine + Chr(Asc(Mid(strFileData, j + 2, 1)) Mod 64 + 32)
Next j
'replace " " with "`"
strTempLine = Replace(strTempLine, " ", "`")
'add encoded line to result buffer
strResult = strResult + strTempLine + vbCrLf
'reset line buffer
strTempLine = ""
Next i
Close intFile
'add the end marker
strResult = strResult & "`" & vbCrLf + "end" + vbCrLf
'asign return value
UUEncodeFile = strResult
End Function
Public Function UUDecodeToFile(strUUCodeData As String, strFilePath As String)
Dim vDataLine As Variant
Dim vDataLines As Variant
Dim strDataLine As String
Dim intSymbols As Integer
Dim intFile As Integer
Dim strTemp As String
'
'remove begin marker
If Left$(strUUCodeData, 6) = "begin " Then
strUUCodeData = Mid$(strUUCodeData, InStr(1, strUUCodeData, vbLf) + 1)
End If
'
'remove end marker
If Right$(strUUCodeData, 5) = "end" + vbCrLf Then
strUUCodeData = Left$(strUUCodeData, Len(strUUCodeData) - 10)
End If
intFile = FreeFile
Open strFilePath For Binary As intFile
'Split encoded data to vDataLines array.
'Now each array member contains a line of encoded data
vDataLines = Split(strUUCodeData, vbCrLf)
For Each vDataLine In vDataLines
'Decode data line by line
'
strDataLine = CStr(vDataLine)
'Get quantity of symbols in a line
intSymbols = Asc(Left$(strDataLine, 1)) - 32
'remove first symbol that just informs
'about quantity of symbols
strDataLine = Mid$(strDataLine, 2)
'replace "`" with " "
strDataLine = Replace(strDataLine, "`", " ")
'Convert every 4-byte chunk to 3-byte chunk by
For i = 1 To Len(strDataLine) Step 4
'1 byte
strTemp = strTemp + Chr((Asc(Mid(strDataLine, i, 1)) - 32) * 4 + _
(Asc(Mid(strDataLine, i + 1, 1)) - 32) \ 16)
'2 byte
strTemp = strTemp + Chr((Asc(Mid(strDataLine, i + 1, 1)) Mod 16) * 16 + _
(Asc(Mid(strDataLine, i + 2, 1)) - 32) \ 4)
'3 byte
strTemp = strTemp + Chr((Asc(Mid(strDataLine, i + 2, 1)) Mod 4) * 64 + _
Asc(Mid(strDataLine, i + 3, 1)) - 32)
Next i
'
strTemp = Left(strTemp, intSymbols)
'write decoded line to the file
Put intFile, , strTemp
'clear buffer for next line
strTemp = ""
Next
Close intFile
End Function
原帖来自于网易社区:http://p5.club.163.com/viewArticleByWWW.m?boardId=vbnbasic&articleId=vbnbasic_102b0c440c25109