那位老大给个把流转换成String的纯字母字符串及反向转回去的代码
如题
[解决办法]
你这个流里是什么数据??是不是就是二进制数据?如果是最简单的就用ba64就行.
[解决办法]
调用 tomcrypt 库的 Base64 编码函数
实现
----------------------------------------------
//---------------------------------------
int __fastcall Bin2Text(String* OutText, TMemoryStream* pMS)
{
*OutText = "";
unsigned long OutStringSize = 4 * (((unsigned long)pMS->Size + 2) / 3) + 1;
OutText->SetLength( OutStringSize );
pMS->Position = 0;
int Result = base64_encode (
(unsigned char*)pMS->Memory, (unsigned long)pMS->Size,
(unsigned char*)OutText->c_str(), &OutStringSize );
return Result;
}
//---------------------------------------
int __fastcall Text2Bin(TMemoryStream* pMS, String* InText)
{
unsigned long pStrSize = InText->Length();
unsigned long StrSize = pStrSize;
pMS->Clear();
pMS->Size = StrSize;
pMS->Position = 0;
int Result = base64_decode (
(unsigned char*)InText->c_str(),
StrSize,
(unsigned char*)pMS->Memory,
(unsigned long*)&pStrSize
);
pMS->Size = pStrSize;
pMS->Position = 0;
return Result;
}
//---------------------------------------
int __fastcall File2Text(String *OutText, String Filename)
{
TMemoryStream* pMS = new TMemoryStream;
pMS->LoadFromFile(Filename);
pMS->Position = 0;
int Result = Bin2Text(OutText, pMS);
delete pMS;
return Result;
}
//---------------------------------------
int __fastcall Text2File(String Filename, String* InText)
{
TMemoryStream* pMS = new TMemoryStream;
int Result = Text2Bin(pMS, InText);
pMS->Position = 0;
pMS->SaveToFile(Filename);
delete pMS;
return Result;
}
//---------------------------------------
----------------
调用
-----------------------------------------------------
String a = "";
File2Text(&a, "1.jpg");
Text2File("2.jpg", &a);