Embarcadero:如何使用TBase64Encoding的EncodeBytesToString方法

时间:2016-04-08 13:16:27

标签: c++ encoding c++builder firemonkey c++builder-xe8

我正在尝试使用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传递给此函数。我在这里缺少什么?

1 个答案:

答案 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);
}
//------------------------------------------------------------------------------