简单代码的奇怪行为 [英] Strange behaviour of simple code

查看:59
本文介绍了简单代码的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我疯了。看看这段代码:


#include< string.h>

#include< stdio.h>

#包括< iostream.h>


使用命名空间std;


char ini_code [2] = {0xFF,0xFE};

char line_sep [2] = {0x20,0x28};

char para_sep [2] = {0x20,0x29};

char end_code [2] = {0xFF,0xFF};

char tab_code [2] = {0x00,0x09};

char alf_code [2] = {0x00,0x0A};

char acr_code [2] = {0x00,0x0D};


int main()

{

char代码[2];

bool gotCR = false;


cin.read(& code [0],2);


code [0] = ini_code [1];

code [1] = ini_code [0];

printf(" 0x%02X %02X \ n,代码[0],代码[1]);

while(cin.read(& code [0],2))

{

if(code [0] == tab_code [1]&& code [1] == tab_code [0])

{

code [0] = line_sep [1];

code [1] = line_sep [0 ];

printf(" 0x%02X%02X \ n,代码[0],代码[1]);

}

else if(code [0] == acr_code [1]&&代码[1] == acr_code [0])

{

gotCR = true;

}

else if(code [0] == alf_code [1]&& code [1] == alf_code [0])

{

if(gotCR)

{

code [0] = para_sep [1];

code [1] = para_sep [0];

printf(0x%02X%02X \ n,代码[0],代码[1]);

}

其他

{

gotCR = false;

}

}

其他

{

printf(" 0x%02X%02X \ n,代码[0],代码[1]);

}

}


code [0] = end_code [1];

code [1] = end_code [0];

printf(0x%02X%02X \ n,代码[0],代码[1]);


返回0;

}


我期待一份清单


0xNNNN

0xNNNN

....

0xNNNN


取而代之的是我得到的东西:


0x004B

0x006F

0x0072

0x0065

0x0061

0x006E

0x0009

0x00FFFFFFC6

0xFFFFFFC5FFFFFFC8

0xFFFFFFC5FFFFFFB5

0xFFFFFFC2FFFFFFC8

0xFFFFFFB2FFFFFFE4

0xFFFFFFB209

0x0009

0x0009

0x0000

0x4800

0x6500

0x6200

0x7200

0x6500


为什么??????


~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~

Dario de Judicibus博士
http:// www.dejudicibus.it/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >

解决方案

Dario deJudicibusescribió:

char ini_code [2] = {0xFF,0xFE};
char line_sep [2] = {0x20,0x28};
char para_sep [2] = {0x20,0x29};
char end_code [2] = {0xFF,0xFF};
char tab_code [2] = {0x00,0x09};
char alf_code [2] = {0x00,0x0A};
char acr_code [2] = {0x00,0x0D};




使用unsigned char。


问候。


< blockquote class =post_quotes> char ini_code [2] = {0xFF,0xFE};
char line_sep [2] = {0x20,0x28};
char para_sep [2] = {0x20,0x29} ;
char end_code [2] = {0xFF,0xFF}; < =当您检测到
时会发生什么? char tab_code [2] = {0x00,0x09};
char alf_code [2] = {0x00,0x0A};
char acr_code [2] = {0x00,0x0D};

int main()
{char code [2];
bool gotCR = false;

cin.read(& code [0],2 );

代码[0] = ini_code [1];
代码[1] = ini_code [0];
printf(" 0x%02X%02X \ n" ;,代码[0],代码[1]);
while(cin.read(& code [0],2))
{
if(code [0] == tab_code [1]&& code [1] == tab_code [0])
{
code [0] = line_sep [1];
code [1] = line_sep [0 ];
printf(0x%02X%02X \ n,代码[0],代码[1]);
}
if if(code [0] == acr_code [1]&& code [1] == acr_code [0])
{
gotCR = true;
}
if if(code [0] == alf_code [1]&& code [1] == alf_code [0])
{
if(gotCR)
{
code [0] = para_sep [1];
代码[1] = para_sep [0];
printf(" 0x%02X%02X \ n,代码[0],cod e [1]);
}
其他
{
gotCR = false; < =这里肯定是假的!
}
其他
{/> printf(" 0x%02X%02X \ n,代码[0 ],代码[1]);
}
}

代码[0] = end_code [1];
代码[1] = end_code [0];
printf(0x%02X%02X \ n,代码[0],代码[1]);

返回0;
}

我期待一个列表

0xNNNN
0xNNNN
...
0xNNNN

相反,我得到的东西如下:

0x004B
0x006F
0x0072
0x0065
0x0061
0x006E
0x0009
0x00FFFFFFC6
0xFFFFFFC5FFFFFFC8 0xFFFFFFC5FFFFFFB5
0xFFFFFFC2FFFFFFC8
0xFFFFFFB2FFFFFFE4
0xFFFFFFB209
0x0009
0x0009
0x0000
0x4800
0x6500
0x6200 0x7200
0x6500

为什么??????
~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
Dr. Dario de Judicibus
http ://www.dejudicibus.it/
~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~




如果没有看到你的输入,就很难了告诉。我有一个问题:为什么你要按照你希望看到的相反顺序定义代码?


它是故意的(出于某种原因我无法想象),还是你的代码在执行

检查错了?


我确实看到至少一个明显的问题:GotCR的处理不是正确的b $ b。在第一次设置为

true后,您永远不会将其设置为false。您将其设置为false的代码位于if(GotCR)的其他位置,其中

表示只有在它变为false时才会设置为false!


看起来你也得到了那些结束的东西。代码,可能

意味着你必须以不同方式处理它们,但是没有代码可以检测到并且

处理它们。与其他代码相同,如标签等。


但是,再次,没有输入,我们无法告诉输出如何获得

肯定是生成的。尝试在调试器中浏览您的应用程序,并查看

每个步骤的变量值。你也可以尝试用

纸来检查你的设计。


-Howard


char ini_code [2] = {0xFF,0xFE};
char line_sep [2] = {0x20,0x28 } char_sep [2] = {0x20,0x29};
char end_code [2] = {0xFF,0xFF};
char tab_code [2] = {0x00,0x09};
char alf_code [2] = {0x00,0x0A};
char acr_code [2] = {0x00,0x0D};


使用unsigned char。



为什么? char类型既不是未签名也不签名,除非明确说明,并且他没有进行任何数学或''>''或''''''比较签名

vs. unsigned可能会产生影响。


我认为问题在于他的逻辑错误且不完整。

(他'没有处理所有情况,而且他正在正确处理CR。)


-Howard


I''m getting crazy. Look at this code:

#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std ;

char ini_code[2] = {0xFF, 0xFE} ;
char line_sep[2] = {0x20, 0x28} ;
char para_sep[2] = {0x20, 0x29} ;
char end_code[2] = {0xFF, 0xFF} ;
char tab_code[2] = {0x00, 0x09} ;
char alf_code[2] = {0x00, 0x0A} ;
char acr_code[2] = {0x00, 0x0D} ;

int main ()
{
char code[2] ;
bool gotCR = false ;

cin.read(&code[0], 2) ;

code[0] = ini_code[1] ;
code[1] = ini_code[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
while (cin.read(&code[0], 2))
{
if (code[0] == tab_code[1] && code[1] == tab_code[0])
{
code[0] = line_sep[1] ;
code[1] = line_sep[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
}
else if (code[0] == acr_code[1] && code[1] == acr_code[0])
{
gotCR = true ;
}
else if (code[0] == alf_code[1] && code[1] == alf_code[0])
{
if (gotCR)
{
code[0] = para_sep[1] ;
code[1] = para_sep[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
}
else
{
gotCR = false ;
}
}
else
{
printf("0x%02X%02X\n",code[0],code[1]);
}
}

code[0] = end_code[1] ;
code[1] = end_code[0] ;
printf("0x%02X%02X\n",code[0],code[1]);

return 0 ;
}

I expect a list of

0xNNNN
0xNNNN
....
0xNNNN

Instead I obtain stuff like:

0x004B
0x006F
0x0072
0x0065
0x0061
0x006E
0x0009
0x00FFFFFFC6
0xFFFFFFC5FFFFFFC8
0xFFFFFFC5FFFFFFB5
0xFFFFFFC2FFFFFFC8
0xFFFFFFB2FFFFFFE4
0xFFFFFFB209
0x0009
0x0009
0x0000
0x4800
0x6500
0x6200
0x7200
0x6500

Why??????

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr. Dario de Judicibus
http://www.dejudicibus.it/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

解决方案

Dario de Judicibus escribió:

char ini_code[2] = {0xFF, 0xFE} ;
char line_sep[2] = {0x20, 0x28} ;
char para_sep[2] = {0x20, 0x29} ;
char end_code[2] = {0xFF, 0xFF} ;
char tab_code[2] = {0x00, 0x09} ;
char alf_code[2] = {0x00, 0x0A} ;
char acr_code[2] = {0x00, 0x0D} ;



Use unsigned char.

Regards.


char ini_code[2] = {0xFF, 0xFE} ;
char line_sep[2] = {0x20, 0x28} ;
char para_sep[2] = {0x20, 0x29} ;
char end_code[2] = {0xFF, 0xFF} ; <= what should happen when you detect this? char tab_code[2] = {0x00, 0x09} ;
char alf_code[2] = {0x00, 0x0A} ;
char acr_code[2] = {0x00, 0x0D} ;

int main ()
{
char code[2] ;
bool gotCR = false ;

cin.read(&code[0], 2) ;

code[0] = ini_code[1] ;
code[1] = ini_code[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
while (cin.read(&code[0], 2))
{
if (code[0] == tab_code[1] && code[1] == tab_code[0])
{
code[0] = line_sep[1] ;
code[1] = line_sep[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
}
else if (code[0] == acr_code[1] && code[1] == acr_code[0])
{
gotCR = true ;
}
else if (code[0] == alf_code[1] && code[1] == alf_code[0])
{
if (gotCR)
{
code[0] = para_sep[1] ;
code[1] = para_sep[0] ;
printf("0x%02X%02X\n",code[0],code[1]);
}
else
{
gotCR = false ; <= this is ALREADY false here!
}
}
else
{
printf("0x%02X%02X\n",code[0],code[1]);
}
}

code[0] = end_code[1] ;
code[1] = end_code[0] ;
printf("0x%02X%02X\n",code[0],code[1]);

return 0 ;
}

I expect a list of

0xNNNN
0xNNNN
...
0xNNNN

Instead I obtain stuff like:

0x004B
0x006F
0x0072
0x0065
0x0061
0x006E
0x0009
0x00FFFFFFC6
0xFFFFFFC5FFFFFFC8
0xFFFFFFC5FFFFFFB5
0xFFFFFFC2FFFFFFC8
0xFFFFFFB2FFFFFFE4
0xFFFFFFB209
0x0009
0x0009
0x0000
0x4800
0x6500
0x6200
0x7200
0x6500

Why??????

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr. Dario de Judicibus
http://www.dejudicibus.it/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Without seeing your input, it''s hard to tell. I have a question: why do
you define the codes in the reverse order that you expect to see them? Is
it intentional (for some reason I can''t imagine), or is your code doing the
checks wrong?

I do see at least one clear problem: The handling of GotCR is not
correct. You never set it to false after the first time it gets set to
true. Your code to set it to false is in the else of an "if (GotCR)", which
means it only gets set to false when it is ALREADY false!

It also looks like you''re getting those "end" codes, which probably
means you have to handle them differently, but there''s no code to detect and
handle them. Same with the other codes, like the tab, etc..

But again, with no input to go by, we can''t tell how the output gets
generated for sure. Try walking through your app in the debugger and see
what the variable values are at each step. You might also try doing it on
paper to check your design.

-Howard



char ini_code[2] = {0xFF, 0xFE} ;
char line_sep[2] = {0x20, 0x28} ;
char para_sep[2] = {0x20, 0x29} ;
char end_code[2] = {0xFF, 0xFF} ;
char tab_code[2] = {0x00, 0x09} ;
char alf_code[2] = {0x00, 0x0A} ;
char acr_code[2] = {0x00, 0x0D} ;

Use unsigned char.


Why? The char type is neither unsigned nor signed unless explicitly
stated, and he''s not doing any math or ''>'' or ''<'' comparisons where signed
vs. unsigned might make a difference.

The problems, I think, are that his logic is incorrect and incomplete.
(He''s not handling all cases, and he''s handling the CR incorrectly.)

-Howard


这篇关于简单代码的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆