Base64 转换器的输出不正确
Base64 converter has incorrect output
我写了这个小转换器来将 Base2 文件数据转换为 Base64。生成的输出不正确,我不知道为什么。如果将图像文件作为输入传入,则问题会变得更加复杂:将随机 NULL 字符 (0x00) 插入到输出中,这永远不会发生。输出应仅包含 Base64Table 的子集(请参阅代码)。
输入:
人不仅在于他的理性,还在于这种不同于其他动物的奇异激情,这是一种心灵的欲望,它通过对持续和不知疲倦的知识一代的坚持不懈的喜悦,超过了任何肉体快乐的短暂激烈。
预期输出:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
(来自Base64上的维基百科页面)
实际输出:
YW7MaXPMZGnMdGnMZ3XMc2jMZCzMbm/MIG/MbHnMYnnMaGnMIHLMYXPMbizMYnXMIGLMIHTMaXPMc2nMZ3XMYXLMcGHMc2nMbiDMcm/MIG/MaGXMIGHMaW3MbHPMIHfMaWPMIGnMIGHMbHXMdCDMZiDMaGXMbWnMZCzMdGjMdCDMeSDMIHDMcnPMdmXMYW7MZSDMZiDMZWzMZ2jMIGnMIHTMZSDMb27MaW7MZWTMYW7MIGnMZGXMYXTMZ2HMbGXMZ2XMZXLMdGnMbiDMZiDMbm/MbGXMZ2XMIGXMY2XMZHPMdGjMIHPMb3LMIHbMaGXMZW7MZSDMZiDMbnnMY2HMbmHMIHDMZWHMdXLMLnLM
法典:
#include <iostream>
#include <fstream>
char Base64Table[64] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', //0-25
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', //26-51
'0','1','2','3','4','5','6','7','8','9', //52-61
'+','/'}; //62-63
char PADDING_CHAR = '=';
char GetFirstSymbol(char* buffer);
char GetSecondSymbol(char* buffer);
char GetThirdSymbol(char* buffer);
char GetFourthSymbol(char* buffer);
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "Incorrect number of arguments." << std::endl;
std::cout << "Press Enter to quit.";
while(!std::cin.get());
return 0;
}
std::cout << "Converting " << argv[1] << std::endl;
std::ifstream input;
input.open(argv[1], std::ios_base::binary);
std::ofstream output;
output.open("data.base64", std::ios_base::binary);
char count[1] = {' '};
unsigned long file_size = 0;
char buffer[3] = {' ', ' ', ' '};
while(input.fail() == false) {
input.read(reinterpret_cast<char*>(count), sizeof(count));
++file_size;
}
input.clear();
input.seekg(0);
while(input.fail() == false) {
input.read(reinterpret_cast<char*>(buffer), sizeof(buffer));
char firstsymbol = GetFirstSymbol(buffer);
char secondsymbol = GetSecondSymbol(buffer);
char thirdsymbol = GetThirdSymbol(buffer);
char fourthsymbol = GetFourthSymbol(buffer);
output.write(reinterpret_cast<char*>(&firstsymbol), sizeof(firstsymbol));
output.write(reinterpret_cast<char*>(&secondsymbol), sizeof(secondsymbol));
if(file_size % 3 == 2) {
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
continue;
} else if(file_size % 3 == 1) {
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
continue;
}
output.write(reinterpret_cast<char*>(&thirdsymbol), sizeof(thirdsymbol));
output.write(reinterpret_cast<char*>(&fourthsymbol), sizeof(fourthsymbol));
}
input.clear();
input.close();
output.clear();
output.close();
return 0;
}
//Gets the 6 most significant digits of the first byte.
char GetFirstSymbol(char* buffer) {
int index = (buffer[1] >> 2);
return Base64Table[index];
}
//Gets the 2 least significant digits from previous (first) byte and 4 most significant from the second byte.
char GetSecondSymbol(char* buffer) {
int index = (((buffer[1] & 0x03) << 4) | ((buffer[2] & 0xF0) >> 4));
return Base64Table[index];
}
//Gets the 4 least significant digits from previous (second) byte and 2 least significant from the third byte.
char GetThirdSymbol(char* buffer) {
int index = (((buffer[2] & 0x0F) << 2) | ((buffer[3] & 0xC0) >> 6));
return Base64Table[index];
}
//Gets the 6 least significant digits from the third byte.
char GetFourthSymbol(char* buffer) {
int index = (buffer[3] & 0x3F);
return Base64Table[index];
}
C/C++ 指针和数组是非常强大的野兽,但它们也会伤害你! 因此,在与他们合作时应小心。 您将buffer
定义为char[3]
但从索引 [1] 访问它到索引 [3],这是您的错误,因为 C/C++ 数组的索引是基于 0 的,根据这一点,您buffer [0][1][2][memory of other variables]
保持不变buffer[0]
而不是写入另一个变量的内存,这是随机错误和无效值的原因!!
相关文章:
- 不正确的比较和交换计数器输出用于快速排序功能
- Windows 10命令提示符的输出不正确
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- C++ - 为什么 unicode 输出不正确?
- 连接的组件程序产生不正确的输出
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 我的叉子输出不正确
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- C++使用 CEIL 和长度输出不正确的值
- 使用 iconv 进行 UTF8 转换的输出不正确
- C strftime()输出不正确
- 不正确的输出和变量未用Eclipse CDT初始化构造函数
- 当行数和列不匹配时,输出不正确
- 合并排序不正确的输出 c++
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++ 嵌套循环输出不正确
- 矩阵和矢量乘法,输出不正确的产品
- 我对这个问题有问题,我的输出不正确
- 测试整数是否在范围内时,逻辑错误和不正确输出