问题描述
我正在Delphi XE5中开发android applicatin,我需要对一些字符串进行BASE64编码和解码.
I am working on android applicatin in Delphi XE5 and I need to BASE64 encode and decode some strings.
此功能对于英文字符来说效果很好,但是我想编码€,$或任何特殊的iso8859-2字符编码却无法正常工作.
This function is working fine for english characters, but what I want to encode €, $ or any special iso8859-2 characters encoding doesn't work.
是否知道如何解决?
我发现BASE64单元 http://www.delphipraxis.net/991-base64-mime-en-decoding.html
I found BASE64 unit http://www.delphipraxis.net/991-base64-mime-en-decoding.html
但是FireMonkey是否支持AnsiString和PAnsiChar类型以及使用该类型的单位?
But does FireMonkey support AnsiString and PAnsiChar type and what unit to include to use this type?
我的代码
uses IdCoderMIME;
...
function Encode64(S: string): string;
var
IdEncoderMIME: TIdEncoderMIME;
begin
try
IdEncoderMIME := TIdEncoderMIME.Create(nil);
Result := IdEncoderMIME.EncodeString(S);
finally
IdEncoderMIME.Free;
end;
end;
function Decode64(S: string): string;
var
IdDecoderMIME: TIdDecoderMIME;
var
IdDecoderMIME: TIdDecoderMIME;
begin
try
IdDecoderMIME := TIdDecoderMIME.Create(nil);
Result := IdDecoderMIME.DecodeString(S);
finally
IdDecoderMIME.Free;
end;
end;
推荐答案
正式,Delphi 不在移动平台上支持AnsiString
和(P)AnsiChar
. 非正式地,它们的支持代码仍存在于编译器和RTL中,只是被隐藏了,因此您无法再访问它.有一个第三方补丁可以重新启用访问权限.
Officially, Delphi DOES NOT support AnsiString
and (P)AnsiChar
on mobile platforms. Unofficially, the support code for them is still present in the compiler and RTL, it is just hidden so you cannot access it anymore. There is a third-party patch available that re-enables access.
在对字符串进行编码/解码时,必须考虑字符编码. Base64编码字节,而不是字符.您必须先将字符串转换为字节序列,然后再对Base64进行编码,然后对Base64进行解码,然后再将其转换回字符串.
When encoding/decoding a string, you have to take character encoding into account. Base64 encodes bytes, not characters. You have to convert a string to a byte sequence before then Base64 encoding the bytes, and then Base64 decode the byte sequence before then converting it back to a string.
TIdEncoderMIME.EncodeString()
和TIdDecoderMIME.DecodeString()
方法具有可选的TIdTextEncoding
或IIdTextEncoding
参数(取决于Indy的版本),用于该字符串<->字节转换.如果未指定文本编码,则Indy将使用其默认文本编码,默认情况下为7位ASCII码(可通过IdGlobal.GIdDefaultTextEncoding
变量配置).
The TIdEncoderMIME.EncodeString()
and TIdDecoderMIME.DecodeString()
methods have an optional TIdTextEncoding
or IIdTextEncoding
parameter (depending on your version of Indy) for that string<->bytes conversion. If you do not specify a text encoding, Indy will use its default text encoding, which is 7bit ASCII by default (configurable via the IdGlobal.GIdDefaultTextEncoding
variable).
例如:
uses
..., IdGlobal, IdCoderMIME;
function Encode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdEncoderMIME.EncodeString(S, ByteEncoding);
end;
function Decode64(const S: string: const ByteEncoding: IIdTextEncoding = nil): string;
begin
Result := TIdDecoderMIME.DecodeString(S, ByteEncoding);
end;
uses
..., IdGlobal;
var
s, base64: string;
begin
s := '€$';
base64 := Encode64(s, IndyTextEncoding_UTF8);
s := Decode64(base64, IndyTextEncoding_UTF8);
end;
uses
..., IdGlobal;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := IndyTextEncoding(28592); // ISO-8859-2
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
end;
uses
..., IdGlobal, IdGlobalProtocols;
var
s, base64: string;
enc: IIdTextEncoding;
begin
enc := CharsetToEncoding('ISO-8859-2');
s := '€$';
base64 := Encode64(s, enc);
s := Decode64(base64, enc);
这篇关于BASE64编码和解码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!