如何从长时间提取字节? [英] How to extract bytes from a long?

查看:63
本文介绍了如何从长时间提取字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶没有人建议工会!


#include< stdio.h>

union _x

{

长lng;

char byt [4];

} X;


void main(void)

{

int i; //总是需要''我',...

长val = 0x12345678;


X.lng = val;

for(i = 3; i> = 0; i--)

{

printf("%#02x \ n",X。 byt [i]);

}

} //主要

I''m suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];
} X;

void main( void )
{
int i; // Always need an ''i'', ...
long val = 0x12345678;

X.lng = val;
for( i = 3; i >= 0; i-- )
{
printf( "%#02x\n", X.byt[i] );
}
} // main

推荐答案

Curt Geske写道:
Curt Geske wrote:
我很惊讶没有人建议工会!

#include< stdio.h>
union _x {
long lng;
char byt [4];
} X;

void main(void)
{
int i ; //总是需要''我',...
长val = 0x12345678;

X.lng = val;
for(i = 3; i> = 0; i--)
{/> printf("%#02x \ n",X.byt [i]);
}

} // main
I''m suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];
} X;

void main( void )
{
int i; // Always need an ''i'', ...
long val = 0x12345678;

X.lng = val;
for( i = 3; i >= 0; i-- )
{
printf( "%#02x\n", X.byt[i] );
}
} // main




我们想要的解决方案并没有表现出未定义的行为,而且非常好。甚至是未指定的行为 - 比如X.byt [0] 0x12或0x78

或0x34或0x56,为什么?


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


Curt Geske< cg ********** @ compuserve.com>写道:


如果你想回复帖子,请使用回复。你的

新闻阅读器的功能,不要开始一个新线程。谢谢。
Curt Geske <cg**********@compuserve.com> wrote:

If you want to reply to a post, please use the "Reply" function of your
newsreader and do not start a new thread. Thank you.
我很惊讶没有人建议工会!

#include< stdio.h>
union _x
{
long lng;
char byt [4];
I''m suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];




就在这里停下来:是什么让你认为很长的是四个字符宽?


< SNIP>


问候

-

Irrwahn

(ir*******@freenet.de)



Stop right here: what makes you think a long is four chars wide?

<SNIP>

Regards
--
Irrwahn
(ir*******@freenet.de)


" Irrwahn Grausewitz" < IR ******* @ freenet.de>在消息中写道

news:bk ******************************** @ 4ax.com ...
"Irrwahn Grausewitz" <ir*******@freenet.de> wrote in message
news:bk********************************@4ax.com...
我很惊讶没有人建议工会!

#include< stdio.h>
联盟_x
{
长lng;
char byt [4];
I''m suprised no one suggested a union!

#include <stdio.h>
union _x
{
long lng;
char byt[4];



停在这里:是什么让你觉得很长的是四个字符宽?



Stop right here: what makes you think a long is four chars wide?




这可以通过sizeof轻松克服。如果您试图以便携式方式执行此操作,则不能依赖

字节顺序。



That could be easily overcome with sizeof. It''s more that you can''t rely on
the byte order if you try to do it portably.


这篇关于如何从长时间提取字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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