moumita said:
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.programming/msg/061db1be797a255f
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