我正在尝试使用EncodeBytesToString
类的TBase64Encoding
方法将字节数组转换为base64编码的字符串。 EncodeBytesToString
状态的documentation:
"返回一个字符串,其输入数组的字节编码为指定的字节数。"
因此,我试图像这样编码我的字节数组:
TFile * File = new TFile();
TBytes Bytes = File->ReadAllBytes("D:\\Sample.pdf");
TBase64Encoding * Encoder = new TBase64Encoding();
String EncodedBytes = Encoder->EncodeBytesToString(Bytes, Bytes.Length);
但是,我收到以下错误:
E2285 Could not find a match for 'TNetEncoding::EncodeBytesToString(TByteDynArray,int)'
我很困惑,因为文档似乎说我应该将TBytes
对象和int
传递给此函数。我在这里缺少什么?
答案 0 :(得分:3)
试试这个:
//------------------------------------------------------------------------------
String __fastcall BytesToBase64( TByteDynArray _ArrayIn )
{
TBase64Encoding * Encoding = new TBase64Encoding( 64, '\n' );
String Result = Encoding->EncodeBytesToString( &_ArrayIn[0], _ArrayIn.High );
delete Encoding;
return Result;
}
//------------------------------------------------------------------------------
TByteDynArray __fastcall Base64ToBytes( String _64String )
{
TByteDynArray My64Bytes = _64String.BytesOf();
return TNetEncoding::Base64->Decode(&My64Bytes[0], My64Bytes.High);
}
//------------------------------------------------------------------------------