[D]【高分悬赏】Python 和 VBScript 对文件编码-解码的方式 (Hex, Base64) 的疑问
本人目前的项目需要在Windows上用VBScript将一文件转换为二进制码存入数据库,再在Linux上用Python从数据库中读取并解码成图片。反复调试,发现微软和Python的转码标准不统一,导致了这种实现方式失败。若有哪位大侠是这方面的高手还请不吝赐教
1. Windows上使用VBScript对图片转码为“base64”格式: Function ReadBinary(FileName) Const adTypeBinary = 1 Dim stream, xmldom, node Set xmldom = CreateObject("Microsoft.XMLDOM") Set node = xmldom.CreateElement("binary") node.DataType = "bin.base64" Set stream = CreateObject("ADODB.Stream") stream.Type = adTypeBinary stream.Open stream.LoadFromFile FileName node.NodeTypedValue = stream.Read stream.Close Set stream = Nothing ReadBinary = node.Text Set node = Nothing Set xmldom = Nothing End Function Public Function WriteFile_Append(pathway,words) Dim fileSystemObj,fileSpec,logFile,way Set fileSystemObj = CreateObject("Scripting.FileSystemObject") fileSpec = pathway Set logFile = fileSystemObj.OpenTextFile(fileSpec, 8, true) logFile.WriteLine (CStr(words)) logFile.Close Set logFile = Nothing End Function hexVal = ReadBinary("c:\desk_2.png") Call WriteFile_Append("c:\ttt.txt", hexVal) 2. 用Python读取文件,并转换回图片格式: f = open('c:\\ttt.txt') img = f.read() f.close() f1 = open('c:\\desk.png', 'a+') f1.write(img.decode('base64')) f1.close()