Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bytes to unsigned long

Reply
Thread Tools

bytes to unsigned long

 
 
moumita
Guest
Posts: n/a
 
      05-09-2007
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.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      05-09-2007
"moumita" <> wrote in message
news: 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.


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-09-2007
On May 9, 8:44 am, moumita <moumitagh...@tataelxsi.co.in> wrote:

> 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:
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

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-09-2007
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

 
Reply With Quote
 
Rennie deGraaf
Guest
Posts: n/a
 
      05-09-2007
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.
>


That's one way to do it, assuming that you've figured out your
endianness and that unsigned long is at least 32 bits on your system.
An alternate method is to use a union, as in something like this:

union ulong_u
{
unsigned long ul;
unsigned char uc[4];
};

//...

ulong_u u;
std::memcpy(&u.uc, &buf, 4);
unsigned long temp = u.ul;

Of course, you may have to shuffle the bytes that you assign to u.uc to
handle endianness correctly.

Rennie deGraaf


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGQYi1IvU5mZP08HERAhBjAJ4mKgI7tfbOKUO2D6Oink KoLti1VwCgroze
CR3c6XXsc29SR7eYeYIZPJ0=
=nUFL
-----END PGP SIGNATURE-----

 
Reply With Quote
 
moumita
Guest
Posts: n/a
 
      05-09-2007
On May 9, 12:21 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "moumita" <moumitagh...@tataelxsi.co.in> wrote in message
>
> news: 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.


thank u all for the reply

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      05-09-2007
Gianni Mariani wrote:
> posting redacted


Please don't post attachments to c.l.c++:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-09-2007
red floyd wrote:
> Gianni Mariani wrote:
> > posting redacted

>
> Please don't post attachments to c.l.c++:
> http://www.parashift.com/c++-faq-lit...t.html#faq-5.4


Rules are meant to be broken....

That one in particular.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-09-2007
On May 9, 7:21 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> > Hi All,
> > I need to convert 4 bytes to an unsigned long.

>
> 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];


This way , and Rennie deGraaf's way, are non-portable. You might
cause a program crash by creating a bit pattern that is not valid for
an unsigned long, and also you don't have any control over what
integer you get out of the bytes you put in.

The only reliable method is the one used in the OP code.
AFAIC any time you have to say "endian might be an issue",
there's something wrong with your algorithm.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-10-2007
On May 9, 9:21 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "moumita" <moumitagh...@tataelxsi.co.in> wrote in message
> news: oups.com...
> > 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.


As may be any number of other issues.

> 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.


The disadvantage of this is that it supposes that the external
representation corresponds exactly to the internal one. You're
"advantage" is actually a serious disadvantage. If the external
format is four bytes, you want to convert exactly four bytes, no
more no less. You don't want to suddenly start reading eight
bytes just because you upgraded your machine, when only four
bytes were read.

--
James Kanze (GABI Software) email:
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Having compilation error: no match for call to ‘(const __gnu_cxx::hash<long long int>) (const long long int&)’ veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Why file containing 256 bytes is 257 bytes long? Yandos C++ 12 09-14-2005 11:53 PM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57