472,808 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,808 software developers and data experts.

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 4714
"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
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
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
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
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
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
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
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
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
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
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
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.