Delphi TMS Web Core中替代System.NetEncoding实现Base64编解码的方案咨询
TMS Web Core中实现Base64编解码的替代方案
在TMS Web Core中,你可以通过以下几种方案实现Base64的编解码,替代FMX中的System.NetEncoding单元:
1. 使用TMS Web Core自带的Web.Encoding单元
TMS Web Core提供了专门的编码处理单元Web.Encoding,其中的TBase64Encoding类可以直接完成Base64的编解码操作,用法和System.NetEncoding中的类逻辑一致。
编码示例:
uses Web.Encoding; function EncodeToBase64(const Input: string): string; var Base64: TBase64Encoding; begin Base64 := TBase64Encoding.Create; try Result := Base64.Encode(Input); finally Base64.Free; end; end;
解码示例:
uses Web.Encoding; function DecodeFromBase64(const Input: string): string; var Base64: TBase64Encoding; begin Base64 := TBase64Encoding.Create; try Result := Base64.Decode(Input); finally Base64.Free; end; end;
2. 调用浏览器原生API
由于TMS Web Core最终编译为JavaScript运行在浏览器中,你可以直接调用浏览器原生的btoa(编码)和atob(解码)方法。需要注意的是,原生API仅支持ASCII字符串,处理中文等非ASCII字符时需要先转成UTF-8格式。
编码示例(含UTF-8处理):
function EncodeToBase64(const Input: string): string; var Utf8Bytes: TBytes; begin Utf8Bytes := TEncoding.UTF8.GetBytes(Input); Result := Window.btoa(TEncoding.UTF8.GetString(Utf8Bytes)); end;
解码示例(含UTF-8处理):
function DecodeFromBase64(const Input: string): string; var Utf8Str: string; Utf8Bytes: TBytes; begin Utf8Str := Window.atob(Input); Utf8Bytes := TEncoding.UTF8.GetBytes(Utf8Str); Result := TEncoding.UTF8.GetString(Utf8Bytes); end;
3. 自定义Base64编解码实现
如果需要完全自主实现,不依赖任何外部单元或浏览器API,可以使用以下自定义函数:
自定义Base64编码函数:
const Base64Chars: array[0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; function CustomBase64Encode(const Input: string): string; var I, J: Integer; B1, B2, B3: Byte; Utf8Bytes: TBytes; begin Utf8Bytes := TEncoding.UTF8.GetBytes(Input); Result := ''; I := 0; while I < Length(Utf8Bytes) do begin B1 := Utf8Bytes[I]; B2 := 0; B3 := 0; if I+1 < Length(Utf8Bytes) then B2 := Utf8Bytes[I+1]; if I+2 < Length(Utf8Bytes) then B3 := Utf8Bytes[I+2]; Result := Result + Base64Chars[B1 shr 2]; Result := Result + Base64Chars[((B1 and 3) shl 4) or (B2 shr 4)]; if I+1 < Length(Utf8Bytes) then Result := Result + Base64Chars[((B2 and 15) shl 2) or (B3 shr 6)] else Result := Result + '='; if I+2 < Length(Utf8Bytes) then Result := Result + Base64Chars[B3 and 63] else Result := Result + '='; Inc(I, 3); end; end;
自定义Base64解码函数:
function CustomBase64Decode(const Input: string): string; var I, J: Integer; C1, C2, C3, C4: Integer; B1, B2, B3: Byte; OutputBytes: TBytes; begin SetLength(OutputBytes, 0); I := 0; while I < Length(Input) do begin while (I < Length(Input)) and (Input[I+1] = ' ') do Inc(I); if I >= Length(Input) then Break; C1 := Pos(Input[I+1], Base64Chars) - 1; Inc(I); C2 := Pos(Input[I+1], Base64Chars) - 1; Inc(I); C3 := -1; if I < Length(Input) then C3 := Pos(Input[I+1], Base64Chars) - 1; Inc(I); C4 := -1; if I < Length(Input) then C4 := Pos(Input[I+1], Base64Chars) - 1; Inc(I); B1 := (C1 shl 2) or (C2 shr 4); SetLength(OutputBytes, Length(OutputBytes)+1); OutputBytes[High(OutputBytes)] := B1; if C3 <> -1 then begin B2 := ((C2 and 15) shl 4) or (C3 shr 2); SetLength(OutputBytes, Length(OutputBytes)+1); OutputBytes[High(OutputBytes)] := B2; end; if C4 <> -1 then begin B3 := ((C3 and 3) shl 6) or C4; SetLength(OutputBytes, Length(OutputBytes)+1); OutputBytes[High(OutputBytes)] := B3; end; end; Result := TEncoding.UTF8.GetString(OutputBytes); end;
内容的提问来源于stack exchange,提问作者Shaun Roselt
相关产品推荐
相关产品推荐

