468,753 Members | 1,177 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,753 developers. It's quick & easy.

Converting from binary to long

All,

Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?

Thank you!

-RB
Jul 22 '05 #1
8 4506
"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote...
Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?


Is your 'long' 8 bytes? Really? And your char is 8 bits?

Usually it's something like

union { long l; char c[sizeof(long)]; } u;
char array[64];
// fill the array somehow
memcpy(u.c, array, sizeof(long));
long id = u.l;

You just need to make sure the order of bytes is correct. If it
is not, reverse the order of bytes in u.c before extracting u.l.

V
Jul 22 '05 #2
"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote...
Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?


Is your 'long' 8 bytes? Really? And your char is 8 bits?

Usually it's something like

union { long l; char c[sizeof(long)]; } u;
char array[64];
// fill the array somehow
memcpy(u.c, array, sizeof(long));
long id = u.l;

You just need to make sure the order of bytes is correct. If it
is not, reverse the order of bytes in u.c before extracting u.l.

V
Jul 22 '05 #3
"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote...
Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?


Is your 'long' 8 bytes? Really? And your char is 8 bits?

Usually it's something like

union { long l; char c[sizeof(long)]; } u;
char array[64];
// fill the array somehow
memcpy(u.c, array, sizeof(long));
long id = u.l;

You just need to make sure the order of bytes is correct. If it
is not, reverse the order of bytes in u.c before extracting u.l.

V
Jul 22 '05 #4

"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote in message
news:9d**************************@posting.google.c om...
All,

Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?

Thank you!


It's very likely that your long is 4 bytes big. If so then this is
impossible. Perhaps you should try two longs? Or maybe your platform has a
64 bit integer type, __int 64 perhaps.

john
Jul 22 '05 #5
Ramiro Barbosa, Jr. wrote:
All,

Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?


First of all you'd have to make sure that sizeof(long)==8
-- this is usually the case only on 64bit machines, but who
knows. Then you'd have to tell us if your machine is big-
of little-endian.

You might be lucky with

memcpy(&id,array,8)

HTH,
- J.
Jul 22 '05 #6

"Ramiro Barbosa, Jr." <ra******@yahoo.com> schrieb im Newsbeitrag
news:9d**************************@posting.google.c om...
All,

Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?

Thank you!


As you mentioned socket I'd use htons() for having a defined byteorder.

Regards
Michael
Jul 22 '05 #7
John,

How would I use two longs? The primitive long in my platform (win2k)
is 4 bytes only!

Thanks,

-RB

"John Harrison" <jo*************@hotmail.com> wrote in message news:<2t*************@uni-berlin.de>...
"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote in message
news:9d**************************@posting.google.c om...
All,

Any ideas on how to convert the first 8 bytes of raw uninterpreted
sequence of bytes from 'char array[64];' (populated with _binary_ data
read from a socket), into a 'long id'?

Thank you!


It's very likely that your long is 4 bytes big. If so then this is
impossible. Perhaps you should try two longs? Or maybe your platform has a
64 bit integer type, __int 64 perhaps.

john

Jul 22 '05 #8

"Ramiro Barbosa, Jr." <ra******@yahoo.com> wrote in message
news:9d**************************@posting.google.c om...
John,

How would I use two longs? The primitive long in my platform (win2k)
is 4 bytes only!

Thanks,

-RB


Well, first four bytes into one long, and the next four bytes into the other
long. Something like

long one = (256L*256L*256L)*(unsigned char)array[0] +
(256L*256L)*(unsigned char)array[1] +
(256L)*(unsigned char)array[2] +
(unsigned char)array[3];
long two = (256L*256L*256L)*(unsigned char)array[4] +
(256L*256L)*(unsigned char)array[5] +
(256L)*(unsigned char)array[6] +
(unsigned char)array[7];

(or the other way round of course).

john
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dan Stromberg | last post: by
4 posts views Thread by Joseph Suprenant | last post: by
5 posts views Thread by nickisme | last post: by
2 posts views Thread by Mariusz Sakowski | last post: by
3 posts views Thread by Pete Davis | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.