Converting from binary to long

  • Thread starter Ramiro Barbosa, Jr.
  • Start date
R

Ramiro Barbosa, Jr.

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
 
V

Victor Bazarov

Ramiro Barbosa said:
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
 
V

Victor Bazarov

Ramiro Barbosa said:
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
 
V

Victor Bazarov

Ramiro Barbosa said:
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
 
J

John Harrison

Ramiro Barbosa said:
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
 
J

Jacek Dziedzic

Ramiro said:
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.
 
M

Michael Kurz

Ramiro Barbosa said:
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
 
R

Ramiro Barbosa, Jr.

John,

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

Thanks,

-RB

John Harrison said:
Ramiro Barbosa said:
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
 
J

John Harrison

Ramiro Barbosa said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top