解码base64会增加额外的空格

发布于 2024-12-06 04:55:54 字数 2279 浏览 0 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

死开点丶别碍眼 2024-12-13 04:55:54

您可以添加一行来获得您期望的长度:

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;

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;
粉红×色少女 2024-12-13 04:55:54

虽然将 UTF7 编码与 Base64 解码混合是一个非常糟糕的主意,但您应该使用 Soap.EncdDecd.pas 中的内置函数 EncodeBase64DecodeBase64

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.

如痴如狂 2024-12-13 04:55:54

首先,为什么要涉及 UTF-7? Base64 是一种仅使用 ASCII 字符的 7 位编码。将 Base64 与 UTF-7 相互转换是不必要的操作。如果您有一个包含 Base64 编码的字符八位组的 TBytes,那么您只需将 TBytes 直接复制到 AnsiString 中,无需进行任何解码all:

var
  byte1: TBytes;
  s: AnsiString;

SetString(s, PAnsiChar(byte1), Length(byte));

或者使用 Indy 的 BytesToStringRaw() 函数:

var
  byte1: TBytes;
  s: String;

s := BytesToStringRaw(byte1);

无论哪种方式,一旦有了 Base64 编码的字符串,就可以消除自定义的字符串Base64Decode() 功能完全通过使用 TIdDecoderMIME.DecodeBytes() 方法来实现:

var
  decoded: TBytes;

decoded := TIdDecoderMIME.DecodeBytes(s);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文