java.unit.base64,BASE64编码和解码不起作用

I am working on android applicatin in Delphi XE5 and I need to BASE64 encode and decode some strings.

This function is working fine for english characters, but what I want to encode €, $ or any special iso8859-2 characters encoding doesn't work.

Any idea how to fix it?

But does FireMonkey support AnsiString and PAnsiChar type and what unit to include to use this type?

My code

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;

解决方案

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.

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.

The TIdEncoderMIME.EncodeString() and TIdDecoderMIME.DecodeString() methods have an optional TIdTextEncoding or IIdTextEncoding parameter (depending on your version of Indy) for that stringbytes 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).

For example:

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值