473,503 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4750
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1896
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
4
16444
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
5
2176
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
2
7657
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
3
2327
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
0
3822
by: ChrisWoodruff | last post by:
I have a C++ function in a COM object that I am trying to implement in VB.NET (the functionality, NOT the COM object, I want to remove the requirement for the COM DLL) I am an experienced VB...
2
4345
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
3
1583
by: Schmacker | last post by:
Hi there, I'm trying to teach myself some things about random file I/O with binary, and have come across a task that I am unsure I know how to approach. Currently I write out a bunch of...
11
3780
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it...
9
4473
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.