字节到无符号长 [英] bytes to unsigned long

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

问题描述

大家好,

我需要将4个字节转换为无符号长。

假设我有一个像unsigned char buf [4]这样的数组。我需要转换

将这4个字节合并为一个

unsigned long。以下是一段代码是正确的吗?或者它是一个

正确的approch吗?


unsigned long temp;

temp =(unsigned long)buff [3];

temp | =((unsigned long)buff [2])<< 8;

temp | =((unsigned long)buff [1])<< 16

temp | =((无符号长)buff [0])<< 24;


等待你的建议。

解决方案

" moumita" < mo ********** @ tataelxsi.co.inwrote in message

news:11 ****************** ****@p77g2000hsh.googlegr oups.com ...


大家好,

我需要将4个字节转换为无符号很长。

假设我有一个像unsigned char buf [4]这样的数组。我需要将这4个字节转换成一个

无符号长。以下是一段代码是正确的吗?或者它是一个

正确的approch吗?


unsigned long temp;

temp =(unsigned long)buff [3];

temp | =((unsigned long)buff [2])<< 8;

temp | =((unsigned long)buff [1])<< 16

temp | =((无符号长)buff [0])<< 24;


等待你的建议。



有几种方法可以做到。我过去做过的一种方法是将
简单地视为一个无符号长整数并加载字节数.Endian

可能是一个问题。


unsigned long temp;

for(int i = 0; i< sizeof(unsigned long); ++ i)

(reinterpret_cast< char *>(& temp))[i] = buff [i];


这样做的好处是它适用于任何大小的unsigned long ,只是

必须确保缓冲区足够长。缓冲区如何加载

unsigned long也可能很重要(大.vs。小端)。


我见过你的方法,然而。


5月9日上午8:44,moumita< moumitagh ... @ tataelxsi.co.inwrote:


我需要将4个字节转换为无符号长。

假设我有一个像unsigned char buf这样的数组[4]。我需要转换

这4个字节成单个

无符号长。下面这段代码是对的??或者它是一个

正确的approch这样做?


unsigned long temp;

temp =(unsigned long)buff [3];

temp | =((unsigned long)buff [2])<< 8;

temp | =((unsigned long)buff [1])<< 16

temp | =((无符号长)buff [0])<< 24;



也许。无论如何,这是正确的方法。问题是这四个字节来自何处
。如果他们来自互联网协议,

它是正确的。


您可能更喜欢使用uint32_t而不是unsigned long。在当前版本的C ++标准中没有出现
,但是它将是下一个版本的一部分,它已经是标准的C,

所以它应该得到大多数编译器的支持(前提是你需要

包括< stdint.h>)。在许多现代机器上,

无符号长是64位。 (这不是真的很重要。)


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l'' école,法国,+ 33(0)1 30 23 00 34


moumita写道:


大家好,

我需要将4个字节转换为无符号长。

假设我有一个像unsigned char buf [4]这样的数组。我需要转换

将这4个字节合并为一个

unsigned long。以下是一段代码是正确的吗?或者它是一个

正确的approch吗?


unsigned long temp;

temp =(unsigned long)buff [3];

temp | =((unsigned long)buff [2])<< 8;

temp | =((unsigned long)buff [1])<< 16

temp | =((无符号长)buff [0])<< 24;


等待你的建议。



您可能需要担心字节序...


我暂时发布了其中一件事......哦,这是。
http://groups.google.com/group/comp=\"1db1be797a255f


我附上一个如何做到这一点的例子。这就是整个生猪,

它允许你简单地重新演绎演员并读取

正确字节顺序中的值。



模板< class base_type,bool wire_is_big_endian = true>

class NetworkOrder

{

public:


base_type m_uav;


静态内联bool EndianCheck()

{

unsigned x = 1;

返回wire_is_big_endian ==! (*(char *)(& x));

}


静态内联无效OrderRead(

const base_type& ; i_val,

base_type& i_destination



{

unsigned char * src =(unsigned char * )& i_val;

unsigned char * dst =(unsigned char *)& i_destination;


if(

(sizeof(base_type)== 1)

|| EndianCheck()

){


//

//对齐是一些架构的问题所以

//即使对于非-swapping我们一次读取一个字节


if(sizeof(base_type)== 1){

dst [0] = src [0];

}否则if(sizeof(base_type)== 2){

dst [0] = src [0];

dst [1 ] = src [1];

}否则if(sizeof(base_type)== 4){

dst [0] = src [0];

dst [1] = src [1];

dst [2] = src [2];

dst [3] = src [3];

}否则{


for(

int i = sizeof(base_type);

i 0 ;

i -

){

*(dst ++)= *(src ++);

}

}


}否则{


if(sizeof(base_type)== 2){

dst [1] = src [0];

dst [0] = src [1];

}否则if(sizeof(base_type) )== 4){

dst [3] = src [0];

dst [2] = src [1];

dst [1] = src [2 ];

dst [0] = src [3];

} else {

dst + = sizeof(base_type)-1;

for(int i = sizeof(base_type);我0;我 - ){

*(dst - )= *(src ++);

}

}

}

}


静态内联无效OrderWrite(

const base_type& i_val,

base_type& i_destination



{

//暂时这与OrderRead相同

OrderRead(i_val,i_destination);

}


内联运算符base_type()const

{

base_type l_value;

OrderRead(m_uav,l_value);

返回l_value;

}


inline base_type operator =(base_type in_val)

{

OrderWrite(in_val,m_uav);

返回in_val;

}


};

#if 1

#include< iostream>


struct wire_data_little_endian

{

NetworkOrder< unsigned long,false a;

};

struct wire_data_big_endian

{

NetworkOrder< unsigned long,true a;

};

int main()

{

{

char buff [5] = {1,2,3,4,0};


wire_data_little_endian& data = * reinterpret_cast< wire_data_little_endian *>(buff);


unsigned long x = data.a;


std :: cout< ;< 小价值 << std :: hex<< x<< " \ n";


data.a = 0x41424344UL;


std :: cout<< 小buff << buff<< " \ n";

}


{

char buff [5] = {1,2,3, 4,0};


wire_data_big_endian& data = * reinterpret_cast< wire_data_big_endian *>(buff);


unsigned long x = data.a;


std :: cout< ;< 大端值 << std :: hex<< x<< " \ n";


data.a = 0x41424344UL;


std :: cout<< big endian buff << buff<< " \ n";

}


}


#endif


Hi All,
I need to convert 4 bytes to an unsigned long.
Suppose I have one array like unsigned char buf[4].I need to convert
these 4 bytes into a single
unsigned long. Is the following piece of code is right??Or is it a
right approch to do that??

unsigned long temp;
temp= (unsigned long) buff[3];
temp | =((unsigned long) buff[2]) << 8;
temp | =((unsigned long) buff[1]) << 16
temp | =((unsigned long) buff[0]) << 24;

Waiting for your suggestions.

解决方案

"moumita" <mo**********@tataelxsi.co.inwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...

Hi All,
I need to convert 4 bytes to an unsigned long.
Suppose I have one array like unsigned char buf[4].I need to convert
these 4 bytes into a single
unsigned long. Is the following piece of code is right??Or is it a
right approch to do that??

unsigned long temp;
temp= (unsigned long) buff[3];
temp | =((unsigned long) buff[2]) << 8;
temp | =((unsigned long) buff[1]) << 16
temp | =((unsigned long) buff[0]) << 24;

Waiting for your suggestions.

There are a few ways to do it. One way I''ve done it in the past is to
simply treat a unsigned long as a char array and load the bytes in. Endian
may be an issue.

unsigned long temp;
for ( int i = 0; i < sizeof( unsigned long ); ++i )
(reinterpret_cast<char*>(&temp))[i] = buff[i];

The advantage of this is that it works on any size of unsigned long, just
gotta make sure the buffer is long enough. How the buffer was loaded with
the unsigned long also may matter (big .vs. little endian).

I''ve seen your method used, however.


On May 9, 8:44 am, moumita <moumitagh...@tataelxsi.co.inwrote:

I need to convert 4 bytes to an unsigned long.
Suppose I have one array like unsigned char buf[4].I need to convert
these 4 bytes into a single
unsigned long. Is the following piece of code is right??Or is it a
right approch to do that??

unsigned long temp;
temp= (unsigned long) buff[3];
temp | =((unsigned long) buff[2]) << 8;
temp | =((unsigned long) buff[1]) << 16
temp | =((unsigned long) buff[0]) << 24;

Maybe. It''s the right approach, anyway. The question is where
the four bytes come from. If they''re from an Internet protocol,
it''s correct.

You might prefer using uint32_t instead of unsigned long. It''s
not present in the current version of the C++ standard, but it
will be part of the next version, and it is already standard C,
so it should be supported by most compilers (provided you
include <stdint.h>, of course). On many modern machines,
unsigned long is 64 bits. (Not that it really matters here.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


moumita wrote:

Hi All,
I need to convert 4 bytes to an unsigned long.
Suppose I have one array like unsigned char buf[4].I need to convert
these 4 bytes into a single
unsigned long. Is the following piece of code is right??Or is it a
right approch to do that??

unsigned long temp;
temp= (unsigned long) buff[3];
temp | =((unsigned long) buff[2]) << 8;
temp | =((unsigned long) buff[1]) << 16
temp | =((unsigned long) buff[0]) << 24;

Waiting for your suggestions.

You may need to worry about endianness...

I posted one of these things a while back ... oh here it is.
http://groups.google.com/group/comp....1db1be797a255f

I attached an example of how you can do it. It''s kind of the whole hog,
it allows you to simply re-interpret cast and read the value in the
correct byte order.


template <class base_type, bool wire_is_big_endian = true >
class NetworkOrder
{
public:

base_type m_uav;

static inline bool EndianCheck()
{
unsigned x = 1;
return wire_is_big_endian == ! ( * ( char * )( & x ) );
}

static inline void OrderRead(
const base_type & i_val,
base_type & i_destination
)
{
unsigned char * src = ( unsigned char * ) & i_val;
unsigned char * dst = ( unsigned char * ) & i_destination;

if (
( sizeof( base_type ) == 1 )
|| EndianCheck()
) {

//
// Alignment is an issue some architectures so
// even for non-swapping we read a byte at a time

if ( sizeof( base_type ) == 1 ) {
dst[0] = src[0];
} else if ( sizeof( base_type ) == 2 ) {
dst[0] = src[0];
dst[1] = src[1];
} else if ( sizeof( base_type ) == 4 ) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = src[3];
} else {

for (
int i = sizeof( base_type );
i 0;
i --
) {
* ( dst ++ ) = * ( src ++ );
}
}

} else {

if ( sizeof( base_type ) == 2 ) {
dst[1] = src[0];
dst[0] = src[1];
} else if ( sizeof( base_type ) == 4 ) {
dst[3] = src[0];
dst[2] = src[1];
dst[1] = src[2];
dst[0] = src[3];
} else {
dst += sizeof( base_type ) -1;
for ( int i = sizeof( base_type ); i 0; i -- ) {
* ( dst -- ) = * ( src ++ );
}
}
}
}

static inline void OrderWrite(
const base_type & i_val,
base_type & i_destination
)
{
// for the time being this is the same as OrderRead
OrderRead( i_val, i_destination );
}

inline operator base_type () const
{
base_type l_value;
OrderRead( m_uav, l_value );
return l_value;
}

inline base_type operator=( base_type in_val )
{
OrderWrite( in_val, m_uav );
return in_val;
}

};
#if 1
#include <iostream>

struct wire_data_little_endian
{
NetworkOrder<unsigned long, false a;
};
struct wire_data_big_endian
{
NetworkOrder<unsigned long, true a;
};
int main()
{
{
char buff[5] = { 1, 2, 3, 4, 0 };

wire_data_little_endian & data = * reinterpret_cast<wire_data_little_endian *>( buff );

unsigned long x = data.a;

std::cout << "little value " << std::hex << x << "\n";

data.a = 0x41424344UL;

std::cout << "little buff " << buff << "\n";
}

{
char buff[5] = { 1, 2, 3, 4, 0 };

wire_data_big_endian & data = * reinterpret_cast<wire_data_big_endian *>( buff );

unsigned long x = data.a;

std::cout << "big endian value " << std::hex << x << "\n";

data.a = 0x41424344UL;

std::cout << "big endian buff " << buff << "\n";
}

}

#endif


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

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