BASE64图像文件用C 编码
Base64 image file encoding with C++
我正在编写一些简单的代码将文件编码为base64。我有一个简短的C 代码,该代码将文件读为向量并将其转换为未签名的char*。我这样做,以便我可以正确使用我获得的编码功能。
问题:它可以与文本文件(不同尺寸(一起使用,但它无法与图像文件一起使用。我无法弄清楚原因。什么给?
对于包含文本abcd
的简单text.txt
,我的代码和bash $( base64 text.txt )
的输出都是相同的。
另一方面,当我输入图像时,输出类似于iVBORwOKGgoAAAAAAA......AAA==
,或者有时以corrupted size vs prev_size Aborted (core dumped)
结尾,前几个字节是正确的。
代码:
static std::vector<char> readBytes(char const* filename)
{
std::ifstream ifs(filename, std::ios::binary|std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
std::vector<char> result(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(&result[0], pos);
return result;
}
static char Base64Digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int ToBase64Simple( const BYTE* pSrc, int nLenSrc, char* pDst, int nLenDst )
{
int nLenOut= 0;
while ( nLenSrc > 0 ) {
if (nLenOut+4 > nLenDst) {
cout << "errorn";
return(0); // error
}
// read three source bytes (24 bits)
BYTE s1= pSrc[0]; // (but avoid reading past the end)
BYTE s2= 0; if (nLenSrc>1) s2=pSrc[1]; //------ corrected, thanks to jprichey
BYTE s3= 0; if (nLenSrc>2) s3=pSrc[2];
DWORD n;
n = s1; // xxx1
n <<= 8; // xx1x
n |= s2; // xx12
n <<= 8; // x12x
n |= s3; // x123
//-------------- get four 6-bit values for lookups
BYTE m4= n & 0x3f; n >>= 6;
BYTE m3= n & 0x3f; n >>= 6;
BYTE m2= n & 0x3f; n >>= 6;
BYTE m1= n & 0x3f;
//------------------ lookup the right digits for output
BYTE b1 = Base64Digits[m1];
BYTE b2 = Base64Digits[m2];
BYTE b3 = Base64Digits[m3];
BYTE b4 = Base64Digits[m4];
//--------- end of input handling
*pDst++ = b1;
*pDst++ = b2;
if ( nLenSrc >= 3 ) { // 24 src bits left to encode, output xxxx
*pDst++ = b3;
*pDst++ = b4;
}
if ( nLenSrc == 2 ) { // 16 src bits left to encode, output xxx=
*pDst++ = b3;
*pDst++ = '=';
}
if ( nLenSrc == 1 ) { // 8 src bits left to encode, output xx==
*pDst++ = '=';
*pDst++ = '=';
}
pSrc += 3;
nLenSrc -= 3;
nLenOut += 4;
}
// Could optionally append a NULL byte like so:
*pDst++= 0; nLenOut++;
return( nLenOut );
}
int main(int argc, char* argv[])
{
std::vector<char> mymsg;
mymsg = readBytes(argv[1]);
char* arr = &mymsg[0];
int len = mymsg.size();
int lendst = ((len+2)/3)*4;
unsigned char* uarr = (unsigned char *) malloc(len*sizeof(unsigned char));
char* dst = (char *) malloc(lendst*sizeof(char));;
mymsg.clear(); //free()
// convert to unsigned char
strncpy((char*)uarr, arr, len);
int lenOut = ToBase64Simple(uarr, len, dst, lendst);
free(uarr);
int cont = 0;
while (cont < lenOut) //(dst[cont] != 0)
cout << dst[cont++];
cout << "n";
}
欢迎任何见解。
我看到了两个问题。
首先,在使用它之前,您要清除mymsg
矢量。这会使arr
指针悬挂(指向不再分配的内存(。当您访问arr
以获取数据时,您最终会出现不确定的行为。
然后,您使用strncpy
复制(可能(二进制数据。当该副本达到文件中的第一个NUL(0(字节时,该副本将停止,因此并非所有数据都会被复制。您应该改用memcpy
。
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 使用文件内容作为硬编码字符串
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 从文件中读取文本时出现编码问题
- 编码大于原始文本:如何获取零和一的字符串并将它们作为实际字节写入文件
- 如何使jse文件执行编码.exe并静默添加启动?Javascript将文件添加到启动?
- 这些PGM文件是如何编码的?
- 算术编码FPAQ0(一个简单的订单-0算术文件压缩机)
- 将 JPG 编码数组从 ROS sensor_msgs/压缩图像保存到 roscpp 中的文件
- 为什么我在有关文件编码格式的QT代码中获得了混乱的代码
- BASE64图像文件用C 编码
- 从编码域中的 MPEG 文件中读取元数据
- C 在使用UCS-2 LE编码的文件中搜索短语
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 管理代码中的硬编码文件路径/名称
- 需要说明如何使用 C++ 在 Linux 上创建 utf-8 编码文件
- 使用ICU(ICU4C)读取UTF-8编码文件的缓冲区大小
- 编译eAccelerator解码PHP编码文件