请摘录 [英] snippet review please

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

问题描述

我是C的新手。我有一段代码可以正常工作,但在输出中有一点点b / b
off。我希望有人能发现我的错误。


我正在读取串口设备的输入,我需要打印Hex,这是$ />
正在工作,但我输出不可靠并且存在格式问题。


示例输出:

FF00000033 FF00000036 FF00000030 FF00000030 FF00000054 FF0000001A
FF0000000F FF00000048 FF00000003 FF0000004A FF00000020 FF00000003

FF00000000 FF00000021 FF00000000 FF00000026 FF00000022 FF00000075

FF00000000 FF00000023 FF00000011 FF00000040 FF00000024 FF0000007F

FF00000040 FF00000025 FF0000000F FF00000050 FF00000026 FF0000007F

FF00000040 FF00000027 FF0000007F FF00000040 FF00000023


HEX。其中大约一半的十六进制翻译是错误的。例如:

最后一行的倒数第二帧; 40应该是C0。


我已尝试过各种printf输出选项,但它们都会吐出

不可靠的输出和格式化问题。我必须跳过一步,或者我需要乘以某种东西来获得



我已经完成了二进制到十六进制的帖子并试过了一些

建议的东西但是现在要么在我头上还是只是

不起作用。


< snippet>

int res;

char buf [255];


retval = select(fd + 1 ,& set,NULL,NULL,& timeout);


if(retval> 0){

res = read(fd,buf, 255);


if(res< = 0){

printf(" Error,res is< = 0");

}


int i;

buf [res] = 0;


for( i = 0; i< res; i ++){

printf("%0LX",(int)(buf [i])); //大多数是

}


< / snippet>


任何建议都将不胜感激。

解决方案


al ** **** @gmail.com 写道:

我是C的新手。我有一段代码可行,但它有点关闭在输出中。我希望有人能发现我的错误。


< snip>

printf("%0LX",(int)(buf [i])); //大多数




如果转换为unsigned int会不会产生任何影响?


< blockquote>

Jason写道:

printf("%0LX",(int)(buf [i])); //大多数



如果你转换成unsigned int会有什么不同吗?




这就是你的意思?


printf("%0LX",(unsigned)(buf [i]));


或者我是无符号的。我试过了两个,结果是一样的。




al ****** @ gmail.com 写道:

我是C的新手。我有一段有用的代码,但它在输出中有点关闭。我希望有人能发现我的错误。


作品的有趣定义...

我正在读取串口设备的输入,我需要打印Hex,它正在工作,但我的输出不可靠,并且存在格式化问题。 br />
示例输出:
FF00000033 FF00000036 FF00000030 FF00000030 FF00000054 FF0000001A
[...]


你的大部分代码都不是标准C,而且我不会对它进行评论(此外,你没有提供足够的背景

来支持很多评论)。但是,这行

普通标准C可能至少部分负责:

printf("%0LX",(int)(buf [i])) ; //大多数




''L''长度修饰符错误。在第一个

的地方,它只适用于'''','''',''e'',''E'',''f'',

''F'',''g''和''g''转换说明符;如果它被使用

与任何其他说明符行为未定义。

第二,L的效果(与适当的

转换)是说相应的参数

是'long double'而不是'double'' - 但你是

提供'int'',两者都不是。


除此之外,`int''错了! 'X''

转换需要'unsigned int''。


解决这两个问题,也许你的程序会

停止工作。


-
Er*********@sun.com


I''m new to C. I have a snippet of code that works, but it''s a little
off in the output. I''m hoping someone can spot my error(s).

I''m reading input from a serial device, and I need to print Hex, which
is working but I''m getting unreliable output and have formating issues.

Example output:
FF00000033 FF00000036 FF00000030 FF00000030 FF00000054 FF0000001A
FF0000000F FF00000048 FF00000003 FF0000004A FF00000020 FF00000003
FF00000000 FF00000021 FF00000000 FF00000026 FF00000022 FF00000075
FF00000000 FF00000023 FF00000011 FF00000040 FF00000024 FF0000007F
FF00000040 FF00000025 FF0000000F FF00000050 FF00000026 FF0000007F
FF00000040 FF00000027 FF0000007F FF00000040 FF00000023

I can''t figure out how to get rid of the FF000000 before the actual
HEX. And in about half of these the hex translation is wrong. Example:
the second to last frame on the last line; 40 should be C0.

I''ve tried various printf output options, but they all spit out
unreliable output with formating issues. I must be skipping a step, or
I need to multiply by something?

I''ve gone through the binary to hex posts and have tried some of the
things suggested but it''s either over my head at this point or just
isn''t working.

<snippet>
int res;
char buf[255];

retval = select(fd+1, &set, NULL,NULL, &timeout);

if (retval > 0) {
res = read(fd,buf,255);

if (res <= 0) {
printf("Error, res is <= 0");
}

int i;
buf[res]=0;

for (i=0;i<res;i++) {
printf("%0LX ", (int)(buf[i])); //mostly
}

</snippet>

Any suggestions would be greatly appreciated.

解决方案


al******@gmail.com wrote:

I''m new to C. I have a snippet of code that works, but it''s a little
off in the output. I''m hoping someone can spot my error(s).
<snip>
printf("%0LX ", (int)(buf[i])); //mostly



Would it make any difference if you cast to an unsigned int instead?



Jason wrote:

printf("%0LX ", (int)(buf[i])); //mostly



Would it make any difference if you cast to an unsigned int instead?



Is this what you meant?

printf("%0LX ", (unsigned)(buf[i]));

Or making i unsigned. I tried both, and the results are the same.




al******@gmail.com wrote:

I''m new to C. I have a snippet of code that works, but it''s a little
off in the output. I''m hoping someone can spot my error(s).
Interesting definition of "works" ...
I''m reading input from a serial device, and I need to print Hex, which
is working but I''m getting unreliable output and have formating issues.

Example output:
FF00000033 FF00000036 FF00000030 FF00000030 FF00000054 FF0000001A
[...]
Much of your code is not Standard C, and I won''t
comment on it (besides, you didn''t give enough context
to support much commentary). However, this line of
plain Standard C may be at least partially responsible:
printf("%0LX ", (int)(buf[i])); //mostly



The ''L'' length modifier is wrong. In the first
place, it only applies to the ''a'', ''A'', ''e'', ''E'', ''f'',
''F'', ''g'', and ''g'' conversion specifiers; if it is used
with any other specifier "the behavior is undefined."
In the second place, the effect of ''L'' (with a proper
conversion) is to say that the corresponding argument
is a `long double'' instead of a `double'' -- but you''re
supplying an `int'', which is neither.

On top of that, the `int'' is wrong! The `X''
conversion requires an `unsigned int''.

Fix those two problems, and maybe your program will
stop "working."

--
Er*********@sun.com


这篇关于请摘录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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