Decoding base64 adds extra spaces
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.
You can add one line to get the length you are expecting:
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;
SetLength(Result, DecodedStm.Size); // add this line
finally
DecodedStm.Free;
end;
finally
Decoder.Free;
end;
end;
While it is a very bad idea to mix UTF7 encoding with Base64 decoding, you should use the built-in functions EncodeBase64
and DecodeBase64
found in Soap.EncdDecd.pas.
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 the TBytes
directly into an AnsiString
without doing any decoding at all:
var
byte1: TBytes;
s: AnsiString;
SetString(s, PAnsiChar(byte1), Length(byte));
Or use Indy's BytesToStringRaw()
function:
var
byte1: TBytes;
s: String;
s := BytesToStringRaw(byte1);
Either way, once you have a base64-encoded string, you can eliminate your custom Base64Decode()
function completely by using the TIdDecoderMIME.DecodeBytes()
method instead:
var
decoded: TBytes;
decoded := TIdDecoderMIME.DecodeBytes(s);
精彩评论