Delphi:如何将 TIdBytes 编码为 Base64 字符串?
如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Indy 的
TIdEncoderMIME
类将String
、TStream
和TIdByte
数据编码为 base64(以及TIdDecoderMIME
从 Base64 解码回String
、TStream
或TIdBytes
),例如:至于发送
AnsiString
数据,D2009+ 中的 Indy 根本没有任何TIdIOHandler.Write()
重载来处理AnsiString
数据,只有UnicodeString数据。要按原样发送
AnsiString
,您可以:1) 使用
RawToBytes()AnsiString
复制到TIdBytes
中code> 然后调用TIdIOHandler.Write(TIdBytes)
:2) 将
AnsiString
数据复制到TStream
中,然后调用TIdIOHandler.Write(TStream)
:或者:
You can use Indy's
TIdEncoderMIME
class to encodeString
,TStream
, andTIdByte
data to base64 (andTIdDecoderMIME
to decode from base64 back toString
,TStream
, orTIdBytes
), eg:As for sending
AnsiString
data, Indy in D2009+ simply does not have anyTIdIOHandler.Write()
overloads for handlingAnsiString
data at all, onlyUnicodeString
data. To send anAnsiString
as-is, you can either:1) copy the
AnsiString
into aTIdBytes
usingRawToBytes()
and then callTIdIOHandler.Write(TIdBytes)
:2) copy the
AnsiString
data into aTStream
and then callTIdIOHandler.Write(TStream)
:Or:
在 Delphi 的更高版本中,默认使用 Unicode 字符串...您应该安全地将返回值简单地转换为 String 来摆脱该警告。 Base64 仅返回一小组值 (ascii) ...这永远不会导致转换为 Unicode 时数据丢失。
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.