Delphi:如何将 TIdBytes 编码为 Base64 字符串?

发布于 2024-12-21 00:58:16 字数 569 浏览 2 评论 0原文

如何将 TIdBytes 编码为 Base64 字符串(不是 AnsiString)?

  ASocket.IOHandler.CheckForDataOnSource(5);

  if not ASocket.Socket.InputBufferIsEmpty then
  begin
    ASocket.Socket.InputBuffer.ExtractToBytes(data);
    // here I need to encode data to base64 string, how ? I don't need AnsiString!!
    // var s:string;
    s := EncodeBase64(data, Length(data)); // but it will be AnsiString :(

或者如何通过 AContext.Connection.Socket.Write() 发送 AnsiString ?

编译器说从'AnsiString'隐式字符串转换为'string'

“data”变量包含来自网站的UTF-8数据。

How to encode TIdBytes to Base64 string (not AnsiString) ?

  ASocket.IOHandler.CheckForDataOnSource(5);

  if not ASocket.Socket.InputBufferIsEmpty then
  begin
    ASocket.Socket.InputBuffer.ExtractToBytes(data);
    // here I need to encode data to base64 string, how ? I don't need AnsiString!!
    // var s:string;
    s := EncodeBase64(data, Length(data)); // but it will be AnsiString :(

Or how to send AnsiString via AContext.Connection.Socket.Write() ?

Compiler saying Implicit string cast from 'AnsiString' to 'string'

"data" variable contains UTF-8 data from website.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清风疏影 2024-12-28 00:58:16

您可以使用 Indy 的 TIdEncoderMIME 类将 StringTStreamTIdByte 数据编码为 base64(以及 TIdDecoderMIME 从 Base64 解码回 StringTStreamTIdBytes),例如:

s := TIdEncoderMIME.EncodeBytes(data);

至于发送AnsiString 数据,D2009+ 中的 Indy 根本没有任何 TIdIOHandler.Write() 重载来处理 AnsiString 数据,只有 UnicodeString数据。要按原样发送 AnsiString,您可以:

1) 使用 RawToBytes()AnsiString 复制到 TIdBytes 中code> 然后调用 TIdIOHandler.Write(TIdBytes)

var
  as: AnsiString;
begin
  as := ...;
  AContext.Connection.IOHandler.Write(RawToBytes(as[1], Length(as)));
end;

2) 将 AnsiString 数据复制到 TStream 中,然后调用TIdIOHandler.Write(TStream)

var
  as: AnsiString;
  strm: TStream;
begin
  strm := TMemoryStream.Create;
  try
    strm.WriteBuffer(as[1], Length(as));
    AContext.Connection.IOHandler.Write(strm);
  finally
    strm.Free;
  end;
end;

或者:

var
  as: AnsiString;
  strm: TStream;
begin
  as := ...;
  strm := TIdMemoryBufferStream.Create(as[1], Length(as));
  try
    AContext.Connection.IOHandler.Write(strm);
  finally
    strm.Free;
  end;
end;

You can use Indy's TIdEncoderMIME class to encode String, TStream, and TIdByte data to base64 (and TIdDecoderMIME to decode from base64 back to String, TStream, or TIdBytes), eg:

s := TIdEncoderMIME.EncodeBytes(data);

As for sending AnsiString data, Indy in D2009+ simply does not have any TIdIOHandler.Write() overloads for handling AnsiString data at all, only UnicodeString data. To send an AnsiString as-is, you can either:

1) copy the AnsiString into a TIdBytes using RawToBytes() and then call TIdIOHandler.Write(TIdBytes):

var
  as: AnsiString;
begin
  as := ...;
  AContext.Connection.IOHandler.Write(RawToBytes(as[1], Length(as)));
end;

2) copy the AnsiString data into a TStream and then call TIdIOHandler.Write(TStream):

var
  as: AnsiString;
  strm: TStream;
begin
  strm := TMemoryStream.Create;
  try
    strm.WriteBuffer(as[1], Length(as));
    AContext.Connection.IOHandler.Write(strm);
  finally
    strm.Free;
  end;
end;

Or:

var
  as: AnsiString;
  strm: TStream;
begin
  as := ...;
  strm := TIdMemoryBufferStream.Create(as[1], Length(as));
  try
    AContext.Connection.IOHandler.Write(strm);
  finally
    strm.Free;
  end;
end;
囚我心虐我身 2024-12-28 00:58:16

在 Delphi 的更高版本中,默认使用 Unicode 字符串...您应该安全地将返回值简单地转换为 String 来摆脱该警告。 Base64 仅返回一小组值 (ascii) ...这永远不会导致转换为 Unicode 时数据丢失。

s := String(EncodeBase64(data, Length(data))); 

In later versions of Delphi with Unicode string as the default...you should be safe to simply cast the return value as a String to rid yourself of that warning. Base64 only returns a small set of values (ascii) ... which will never lead to data loss in conversion to Unicode.

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