解码base64会增加额外的空格
我在Delphi XE2工作 我正在使用此函数将 base64 字节数组转换为字符串
TEncoding.UTF7.GetString(byte1)
,然后使用此函数将字符串解码为字节数组
function Base64Decode(const EncodedText: string): TBytes;
var
DecodedStm: TBytesStream;
Decoder: TIdDecoderMIME;
begin
Decoder := TIdDecoderMIME.Create(nil);
try
DecodedStm := TBytesStream.Create;
try
Decoder.DecodeBegin(DecodedStm);
Decoder.Decode(EncodedText);
Decoder.DecodeEnd;
Result := DecodedStm.Bytes;
finally
DecodedStm.Free;
end;
finally
Decoder.Free;
end;
end;
,但输入字符串长度为 400,解码后其字节数组长度为 8192。
在某个地方添加了额外的空格...有什么建议吗?我做错了吗?
编辑...
这是我通过 tcp 从 c# 发送到 delphi XE2 应用程序的数据的 base64 字符串长度= 400
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
当我将TBytes转换为字符串时,我在delphi应用程序中收到TBytes,下面是
Tbytes to UTF-7 string 字符串长度 = 401
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
TByte 转 UTF-8 字符串 字符串长度= 0 (零)
TByte 到 ANSI 字符串 字符串长度= 401
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
之间有 1 个额外的位,但我不知道为什么会出现。
善意的建议我做错了什么吗?
I am working in Delphi XE2
I am converting base64 byte array to string using this
TEncoding.UTF7.GetString(byte1)
Then decoding string into byte array using this function
function Base64Decode(const EncodedText: string): TBytes;
var
DecodedStm: TBytesStream;
Decoder: TIdDecoderMIME;
begin
Decoder := TIdDecoderMIME.Create(nil);
try
DecodedStm := TBytesStream.Create;
try
Decoder.DecodeBegin(DecodedStm);
Decoder.Decode(EncodedText);
Decoder.DecodeEnd;
Result := DecodedStm.Bytes;
finally
DecodedStm.Free;
end;
finally
Decoder.Free;
end;
end;
But input string length is 400 and after decode its byte array length is 8192.
Somewhere it is adding extra spaces... any suggestion as what I'm doing wrong?
Edit...
this is base64 of my data send from c# through tcp to delphi XE2 app
string length = 400
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
i receive TBytes in my delphi app when i convert TBytes to string following comes
Tbytes to UTF-7 string
string length = 401
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
TByte to UTF-8 String
string length= 0 ( ZERO)
TByte to ANSI String
string length= 401
TVpQAAIAAAAEAA8A//8AALgAAAAAAAAAQAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAALoQAA4ftAnNIbgBTM0hkJBUaGlzIHByb2dyYW0gbXVzdCBiZSBydW4gdW5kZXIgV2luMzINCiQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBFAABMAQgAGV5CKgAAAAAAAAAA4ACOgQsBAhkArAoAAMQCAAAAAAAwugoA
there is 1 extra bit is comming in between but i dont know why it is comming.
kindly advice am i doing any thing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加一行来获得您期望的长度:
You can add one line to get the length you are expecting:
虽然将 UTF7 编码与 Base64 解码混合是一个非常糟糕的主意,但您应该使用 Soap.EncdDecd.pas 中的内置函数
EncodeBase64
和DecodeBase64
。While it is a very bad idea to mix UTF7 encoding with Base64 decoding, you should use the built-in functions
EncodeBase64
andDecodeBase64
found in Soap.EncdDecd.pas.首先,为什么要涉及 UTF-7? Base64 是一种仅使用 ASCII 字符的 7 位编码。将 Base64 与 UTF-7 相互转换是不必要的操作。如果您有一个包含 Base64 编码的字符八位组的
TBytes
,那么您只需将TBytes
直接复制到AnsiString
中,无需进行任何解码all:或者使用 Indy 的
BytesToStringRaw()
函数:无论哪种方式,一旦有了 Base64 编码的字符串,就可以消除自定义的字符串
Base64Decode()
功能完全通过使用TIdDecoderMIME.DecodeBytes()
方法来实现:First and foremost, why are you involving UTF-7 at all? Base64 is a 7-bit encoding that uses only ASCII characters. Converting base64 to/from UTF-7 is an unnecessary operation. If you have a
TBytes
that contains base64-encoded character octets, then you can simply copy theTBytes
directly into anAnsiString
without doing any decoding at all:Or use Indy's
BytesToStringRaw()
function:Either way, once you have a base64-encoded string, you can eliminate your custom
Base64Decode()
function completely by using theTIdDecoderMIME.DecodeBytes()
method instead: