MSVC中压缩后可能出现的数据损坏?(C+;+;) 我对C++有点陌生,我正在忙着基本的压缩。我在MSVC(Windows7,编译为32位控制台程序)中编写了下面的程序,它将具有4个可能值的字符数组压缩为一个字节。我已经包含了检查中间二进制值的代码行
(抱歉下面的代码太长,只包含iostream) 程序运行时: ABCD转换为11100100,根据我的编码表,这是正确的。 这将在我的系统上转换为ASCIIý 然而,当解码时,ý变为11101100,解码为“ADCD”!我尝试过其他一些起始数组,只有当数组中的第二个字符是“B”时,当它被更改为“D”时,或者如果有一个包含所有“B”的字符串,当备用“B”被更改为“D”时,才会发生损坏。当放置在其他位置时,“B”不会损坏 如果有人能给我一些提示的话,我不明白为什么一个位会出错,而且只会针对特定的序列 谢谢 KMSVC中压缩后可能出现的数据损坏?(C+;+;) 我对C++有点陌生,我正在忙着基本的压缩。我在MSVC(Windows7,编译为32位控制台程序)中编写了下面的程序,它将具有4个可能值的字符数组压缩为一个字节。我已经包含了检查中间二进制值的代码行,c++,visual-c++,visual-studio-2012,compression,C++,Visual C++,Visual Studio 2012,Compression,(抱歉下面的代码太长,只包含iostream) 程序运行时: ABCD转换为11100100,根据我的编码表,这是正确的。 这将在我的系统上转换为ASCIIý 然而,当解码时,ý变为11101100,解码为“ADCD”!我尝试过其他一些起始数组,只有当数组中的第二个字符是“B”时,当它被更改为“D”时,或者如果有一个包含所有“B”的字符串,当备用“B”被更改为“D”时,才会发生损坏。当放置在其他位置时,“B”不会损坏 如果有人能给我一些提示的话,我不明白为什么一个位会出错,而且只会针对特定的序列
struct CompressedChar{
int第一位;
int第二位;
};
压缩字符编码器(字符基输入)
{
压缩字符位输出;
开关(基本输入)
{
案例“A”:
bitoutput.firstbit=0;
bitoutput.secondbit=0;
打破
案例“B”:
bitoutput.firstbit=1;
bitoutput.secondbit=0;
打破
案例“C”:
bitoutput.firstbit=0;
bitoutput.secondbit=1;
打破
案例“D”:
bitoutput.firstbit=1;
bitoutput.secondbit=1;
打破
}
返回位输出;
}
字符解码器(int第一位,int第二位)
{
如果(第一位==0)
{
如果(第二位==0)
返回“A”;
else if(第二位==1)
返回“C”;
}
else if(第一位==1)
{
如果(第二位==0)
返回“B”;
else if(第二位=1)
返回“D”;
}
返回“0”;
}
int main()
{
字符a[4]={'a','B','C','D'};
字符输出;
对于(int i=0;i<8;i+=2)
{
压缩字符位输出;
比特输出=编码器(a[(i/2)];
std::cout这是因为它被初始化为0xCC。您可能在第行中犯了一个错误,该行显示“else if(bitoutput.firstbit==0)”应该是第二位。还要将输出无符号字符设置为安全/清晰。谢谢!已修复!我知道我一定做了一些粗心的事情……尽管如此,我仍然不明白为什么错误只针对“B”字符。这是因为它被初始化为0xCC。您可能在第行中也犯了一个错误,该行上写着“否则如果”(bitoutput.firstbit==0)“应为第二位。还应将输出无符号字符设置为安全/清除。
struct CompressedChar {
int firstbit;
int secondbit;
};
CompressedChar Encoder(char baseinput)
{
CompressedChar bitoutput;
switch (baseinput)
{
case 'A':
bitoutput.firstbit = 0;
bitoutput.secondbit = 0;
break;
case 'B':
bitoutput.firstbit = 1;
bitoutput.secondbit = 0;
break;
case 'C':
bitoutput.firstbit = 0;
bitoutput.secondbit = 1;
break;
case 'D':
bitoutput.firstbit = 1;
bitoutput.secondbit = 1;
break;
}
return bitoutput;
}
char Decoder(int firstbit, int secondbit)
{
if (firstbit == 0)
{
if (secondbit == 0)
return 'A';
else if (secondbit == 1)
return 'C';
}
else if (firstbit == 1)
{
if (secondbit == 0)
return 'B';
else if (secondbit = 1)
return 'D';
}
return '0';
}
int main()
{
char a[4] = {'A', 'B', 'C', 'D'};
char output;
for (int i = 0; i < 8; i += 2)
{
CompressedChar bitoutput;
bitoutput = Encoder(a[(i/2)]);
std::cout << bitoutput.firstbit;
std::cout << bitoutput.secondbit;
if (bitoutput.firstbit == 1)
{ output |= (1 << i); }
else if (bitoutput.firstbit == 0)
{ output &= ~(1 << i);}
if (bitoutput.secondbit == 1)
{ output |= (1 << (i + 1) ); }
else if (bitoutput.firstbit == 0)
{ output &= ~(1 << (i + 1));}
}
std::cout << std::endl << output << std::endl;
char b[4];
int temp1, temp2;
for (int i = 0; i < 8; i += 2)
{
temp1 = (output >> i) & 1;
temp2 = (output >> (i + 1)) & 1;
std::cout<< temp1;
std::cout<< temp2;
b[i/2] = Decoder(temp1, temp2);
}
std::cout<< std::endl;
for (int j = 0; j < 4; j ++)
{
std::cout << b[j];
}
std::cout << std::endl;
return 0;
}