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

Posted on 2005-04-06
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];

       more code.....


    How do I get the right values for each from the array pbuffer????
Question by:lcrogers
    LVL 9

    Accepted Solution

    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

    LVL 55

    Assisted Solution

    by:Jaime Olivares
    You need some casting:

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

    LVL 16

    Assisted Solution

    You could try a memcpy:

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

    Expert Comment

    by:Jaime Olivares
    No asker's feedback...

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    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++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now