字节数组长? [英] byte array and long??

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

问题描述

你好!

我有以下情况:

我有一个字节数组,在某个位置存储4个字节,并且

这些应该是放。到长变量(或任何其他4字节一个)。

ie:

byte a [] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66, 0,0

和字节0xfe,0x30,0x9e,0x2应该放入长变量,

应该包含43921662

如何要实现这个目标吗?


谢谢!

hello!
I have a following situation:
I have a byte array where at a certain location are stored 4 bytes, and
these should be "put" into long variable (or any other 4 byte one).
ie:
byte a[]={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which
should then contain 43921662
how to achieve this?

thanks!

推荐答案



" w3r3w0lf" ; <关于** @ asdf.asdf>在消息中写道

新闻:bv ********** @ ls219.htnet.hr ...

"w3r3w0lf" <as**@asdf.asdf> wrote in message
news:bv**********@ls219.htnet.hr...
你好!
我有一个以下情况:
我有一个字节数组,在某个位置存储4个字节,并且
这些应该是put。到长变量(或任何其他4字节一个)。
即:
字节a [] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
并且字节0xfe,0x30,0x9e,0x2应该放入长变量,然后
应该包含43921662
如何实现这个?

谢谢!
hello!
I have a following situation:
I have a byte array where at a certain location are stored 4 bytes, and
these should be "put" into long variable (or any other 4 byte one).
ie:
byte a[]={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which
should then contain 43921662
how to achieve this?

thanks!




这个*可能*工作,


long x = * reinterpret_cast< long *>(a + 2);


但是如果它不是因为你必须交换周围的字节。


byte tmp [4] = {a [5],a [ 4],a [3],a [2]}; //交换字节

long x = * reinterpret_cast< long *>(tmp);


john



This *might* work,

long x = *reinterpret_cast<long*>(a + 2);

but if it doesn''t it is because you have to swap the bytes around.

byte tmp[4] = { a[5], a[4], a[3], a[2] }; // swap bytes
long x = *reinterpret_cast<long*>(tmp);

john


2004年2月3日星期二22:28:38 -0000,John Harrison

< jo ************* @ hotmail。 COM>在comp.lang.c ++中写道:
On Tue, 3 Feb 2004 22:28:38 -0000, "John Harrison"
<jo*************@hotmail.com> wrote in comp.lang.c++:

" w3r3w0lf" <关于** @ asdf.asdf>在消息中写道
新闻:bv ********** @ ls219.htnet.hr ...

"w3r3w0lf" <as**@asdf.asdf> wrote in message
news:bv**********@ls219.htnet.hr...
你好!
我有以下情况:<我有一个字节数组,在某个位置存储4个字节,并且这些应该是put。到长变量(或任何其他4字节一个)。
即:
字节a [] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
并且字节0xfe,0x30,0x9e,0x2应该放入长变量,然后
应该包含43921662
如何实现这个?

谢谢!
hello!
I have a following situation:
I have a byte array where at a certain location are stored 4 bytes, and
these should be "put" into long variable (or any other 4 byte one).
ie:
byte a[]={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which
should then contain 43921662
how to achieve this?

thanks!



这*可能*工作,

长x = * reinterpret_cast< long *>(a + 2);

但如果它没有''这是因为你必须交换周围的字节。

字节tmp [4] = {a [5],a [4],a [3],a [2]}; //交换字节
long x = * reinterpret_cast< long *>(tmp);

john



This *might* work,

long x = *reinterpret_cast<long*>(a + 2);

but if it doesn''t it is because you have to swap the bytes around.

byte tmp[4] = { a[5], a[4], a[3], a[2] }; // swap bytes
long x = *reinterpret_cast<long*>(tmp);

john




但它会由于不正确的对齐而导致ARM(以及其他一些处理器)因故障而导致故障处理失败。


因为长期意愿,它将在TI 320F28xx上失败包含0x003000fe

这不是OP所要求的。


实际上它在任何

平台上都是未定义的行为/建筑/实施。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://www.eskimo.com/~scs/ C-faq / top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
<一个rel =nofollowhref =http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html\"target =_ blank> http://www.contrib.andrew.cmu .edu / ~a ... FAQ-acllc.html



But it will fail on an ARM (and some other processors) with an
addressing fault due to improper alignment.

It will fail on a TI 320F28xx because the long will contain 0x003000fe
which is not what the OP asked for.

And in fact it is undefined behavior on any
platform/architecture/implementation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


" w3r3w0lf" <关于** @ asdf.asdf>在消息中写道

新闻:bv ********** @ ls219.htnet.hr ...
"w3r3w0lf" <as**@asdf.asdf> wrote in message
news:bv**********@ls219.htnet.hr...
你好!
我有一个以下情况:
我有一个字节数组,在某个位置存储4个字节,并且
这些应该是put。到长变量(或任何其他4字节一个)。
即:
字节a [] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
并且字节0xfe,0x30,0x9e,0x2应该放入长变量中,然后
应该包含43921662
如何实现这个?

谢谢!
hello!
I have a following situation:
I have a byte array where at a certain location are stored 4 bytes, and
these should be "put" into long variable (or any other 4 byte one).
ie:
byte a[]={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0}
and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which
should then contain 43921662
how to achieve this?

thanks!




尝试这个并告诉我它是否有效!


#include< iostream>

使用namespace std;


int main(){

char a [] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0, 0};

长b = 0;


memcpy(& b,& a [2],sizeof(4));


cout<< b<< endl;

cin.get();

}


~让我们linux~

----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000个新闻组--19个不同的服务器! = -----



try this and tell me if this works!

#include <iostream>
using namespace std;

int main(){
char a[] = {0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0};
long b=0;

memcpy(&b,&a[2], sizeof(4));

cout << b << endl;
cin.get();
}

~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


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

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