?
Solved

From a BYTE array how do I assign the values to a long or short?

Posted on 2005-04-06
7
Medium Priority
?
214 Views
Last Modified: 2010-04-24
I read a binary file into a byte array and now I am trying to assign the values to longs, integers, and ulongs variables.  

  for (int x=0; x,filesize; x++)
  {
      if (pbuffer[x] == 0x40 && pbuffer[x+1] == 0x40 && pbuffer[x+2] != 0x40)
      {
           ULONG eventtime;
           double north, east;
           int       pid;

           eventtime = pbuffer[x+3];
           pid           = pbuffer[x+7];
           north        = pbuffer[x+15];
          etc....

       more code.....

        }

    How do I get the right values for each from the array pbuffer????
0
Comment
Question by:lcrogers
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
rcarlan earned 129 total points
ID: 13722258
I assume the pbuffer is declared like so: BYTE* pbuffer
I also assume you meant to write: for(int x=0; x<filesize; x++)

If so, do this:

unsigned nCurrOffset = x+3;
eventtime = *(ULONG*)(pbuffer+nCurrOffset);  nCurrOffset+=sizeof(ULONG);
pid = *(int*)(pbuffer+nCurrOffset);  nCurrOffset+=sizeof(int);
north = *(double*)(pbuffer+nCurrOffset);  nCurrOffset+=sizeof(double);
// etc


You could also define a struct with byte alignment, like so:

#pragma pack(push)
#pragma pack(1)
struct SMyData
{
      ULONG eventtime;
      int pid;
      double north;
      // etc
};
#pragma pack(pop)

Then you can simply do:

SMyData* pData = (SMyData*)(pbuffer+x+3);
pData->eventtime;   // do something with the value
pData->pid;   // do something with the value
pData->north;   // do something with the value
// etc

Radu
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 123 total points
ID: 13722271
You need some casting:

eventtime = *((ULONG *)(pbuffer+x+3));
pid           = *((int)(pbuffer+x+7));
north        = *((double)(pbuffer+x+15));

0
 
LVL 16

Assisted Solution

by:nonubik
nonubik earned 123 total points
ID: 13724380
You could try a memcpy:

memcpy(eventtime, pbuffer+x+3, sizeof(eventtime));
memcpy(pid, pbuffer+x+7, sizeof(pid));
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13917004
No asker's feedback...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question