473,651 Members | 2,496 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 4769
"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.goo gle.com...
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,arra y,8)

HTH,
- J.
Jul 22 '05 #6

"Ramiro Barbosa, Jr." <ra******@yahoo .com> schrieb im Newsbeitrag
news:9d******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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.goo gle.com...
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)*(un signed char)array[1] +
(256L)*(unsigne d char)array[2] +
(unsigned char)array[3];
long two = (256L*256L*256L )*(unsigned char)array[4] +
(256L*256L)*(un signed char)array[5] +
(256L)*(unsigne d 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
1908
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 at: http://dcs.nac.uci.edu/~strombrg/converting-binary.html So far, the page includes a variety of programs written in C or python to do:
4
16458
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 someone can help Thanks
5
2208
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 numbers into 16 bit binary form, so that I can eventually alter the LSB for dithering purposes. Obviously I can create my own function that will do this, but I was wondering if there was some quick process that I could call up from a library that...
2
7666
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 int type). I want to store the number as binary stream, and I don't know how to convert large decimal string (char*) to binary stream. Converting using MOD would last for hours with really big numbers, and in fact I don't know any other ways to...
3
2336
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 need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this in C#? Thanks.
0
3830
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 programmer, but this is my first .NET app... Original Code (what I have been doing and works) VB6 (extra stuff removed for clarity) Set objWbem = CreateObject("WbemScripting.SWbemLocator") Set objSvc = objWbem.ConnectServer(ComputerName, "root")
2
4365
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the fields I decode tells me which the latter fields are). The code I have converts a single byte of...
3
1593
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 variable length structs to a binary file. They look something like this struct testStruct { std::string term;
11
3811
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 doesn't seem to work. The code below is what I'm trying to use. Anyone see any obvious errors? or have any hints/pointers? regards, Igor float floatRead = 0;
9
4493
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 function: x = new int;
0
8352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.