我正试图在Ascii85中开发一个c++解码器,以便从adobe文件(*ai)中解析位图文件。
我在java 这里中找到了一个算法,并尝试用c++重写它。
问题是,我有一些情况,我的编码文本没有被正确解码。例如,如果字符"a“是字符串中的第7个也是最后一个字符(在编码之前),则作为结果,它被解码为"", which is the previous character in the ascii table. It is weird because I have tried to make the calculations of the algorithm manually, and I get "”。我想知道算法中是否有错误,或者它是否是adobe ascii85解码的正确算法。
这是我的代码:
#include <QCoreApplication>
#include <stdio.h>
#include <string.h>
#include <QDebug>
// returns 1 when there are no more bytes to decode
// 0 otherwise
int decodeBlock(char *input, unsigned char *output, unsigned int inputSize) {
qDebug() << input << output << inputSize;
if (inputSize > 0) {
unsigned int bytesToDecode = (inputSize < 5)? inputSize : 5;
unsigned int x[5] = { 0 };
unsigned int i;
for (i = 0; i < bytesToDecode; i++) {
x[i] = input[i] - 33;
qDebug() << x[i] << ", i: " << i;
}
if (i > 0)
i--;
unsigned int value =
x[0] * 85 * 85 * 85 * 85 +
x[1] * 85 * 85 * 85 +
x[2] * 85 * 85 +
x[3] * 85 +
x[4];
for (unsigned int j = 0; j < i; j++) {
int shift = 8 * (3 - j); // 8 * 3, 8 * 2, 8 * 1, 8 * 0
unsigned char byte = (unsigned char)((value >> shift) & 0xff);
printf("byte: %c, %d\n", byte, byte);
*output = byte;
output++;
}
}
return inputSize <= 5;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char x__input[] = "<~FE1f+@;K?~>";
unsigned char x__output[128] = { 0 };
char *input = x__input + 2;
unsigned int inputSize = (unsigned int)strlen(input);
inputSize -= 2;
unsigned char *output = x__output;
printf("decoding %s\n", input);
for (unsigned int i = 0; i < inputSize; i += 5, input += 5, output += 4)
if(decodeBlock(input, output, inputSize - i))
break;
printf("Output is: %s\n", x__output);
return a.exec();
}发布于 2014-12-15 14:44:23
当inputSize不是5的倍数时会发生什么??
unsigned int bytesToDecode = (inputSize < 5)? inputSize : 5;假设bytesToDecode为5,而有些字节的值未知
所以,当你的角色是第7位的最后一位时,上述条件是正确的。
如果输入不是5的倍数,则必须加上值"u“。有关编码/解码过程的更多细节,请查看Wikipedia页面,在该页面中解释得很清楚:
Ascii85
https://stackoverflow.com/questions/27486372
复制相似问题