Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:15:18+00:00 2026-06-02T23:15:18+00:00

I’m working on a project where I need to communicate with a device via

  • 0

I’m working on a project where I need to communicate with a device via the TCP/IP-protocol.
The device sends an large amount of data which I somehow want to parse into some objects/structs.

Datapackage example (in the TCP buffer[]):

[64] [1] [78] [244] [77] [189] [249] [149] hcurrent
[64] [1] [78] [247] [89] [95] [104] [85] htarget
[0] [0] [0] [0] [0] [0] [0] [0] qcurrent
[188] [220] [97] [3] [66] [62] [0] [0] kcurrent
[66] [0] [102] [103] [66] [99][153] [154] mcurrent
[253] [191] [246] [74] [170] [216] [242] [29] fmode
[102] [191] [246] [74] [178] [44] [92] [72] tmil
[137] mode

Now this package frame is identified as:

 double hcurrent
 double htarget
 double qcurrent
 float kcurrent
 float mcurrent
 float fmode
 float tmil
 unsigned char mode

My idea was that I somehow could parse the data directly into a struct which had the same structure as above.
Of course it will be neccessary to identify some key values to determine which kind of data it is.

How can this be done?

Since I’m coding for an iOS device it has to be objective-C or C(++).

EDIT (method tested for copying each part of the datagram into struct):
Small Java implementation where i try to read the first 4 bytes [0] [0] [1] [5]:

byte[] read = new byte[4]; 
int length = 0;
while (length < read.length) {
    len = iStream.read(read, len, read.length);
}
int ByteLength = (int)unsignedIntToLong(read);
ByteLength = ByteLength-5;
state = 1; // Continue and work with next data.

And the bit manipulation method:

public long unsignedIntToLong(byte[] b) 
{
    long l = 0;
    l |= b[0] & 0xFF;
    l <<= 8;
    l |= b[1] & 0xFF;
    l <<= 8;
    l |= b[2] & 0xFF;
    l <<= 8;
    l |= b[3] & 0xFF;
    return l;
}

So i fetch the first 4 bytes i mentioned earlier which determines something specific, and in the end an find the length of 465. My plan is to repeat this process with all other parts of the received data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:15:20+00:00Added an answer on June 2, 2026 at 11:15 pm

    The biggest problem you’re going to have is that structs don’t store data in a completely contiguous form, they align data acording to word boundaries

    This means that you can’t simply define a struct and then cast your buffer[] to it if the buffer didn’t contain a struct to begin with. Instead what you probably need to do is declare a struct and then memcpy each part of the buffer[] in one field at a time using apointer offset into the buffer[].

    If this approach is too cumbersome, it’s often possible to turn off structure alignment so that a structure can represent completely packed data. MSVC allows the use of #pragma pack to do this. This approach does however slow memory access to the structure.

    EDIT: Here’s an example which shows how you can use a template function to read any type from a buffer and then update an offset into that buffer. You can use this method to safely parse any number of types into a structure one by one:

    // We want to copy raw data to this structure
    // but the short will cause it to be unaligned
    struct _parsed_structure
    {
        int a;
        int b;
        short c;
        int d;
    } parsed_structure;
    
    template<typename T>
    void read_and_update_offset (int & offset, char * buffer, T & var)
    {
        T * pInt = (T*)(buffer + offset);
        var = *pInt;
        offset += sizeof(T);
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        // Here's a buffer which we know contains ints and shorts, we could just cast it to our structure
        // but this will cause errors because the structure will not be aligned properly.
        char buffer[] = { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0 };
    
        // Read the first int from the buffer into the structure
        int offset = 0;
        read_and_update_offset(offset, buffer, parsed_structure.a);
        read_and_update_offset(offset, buffer, parsed_structure.b);
        read_and_update_offset(offset, buffer, parsed_structure.c);
        read_and_update_offset(offset, buffer, parsed_structure.d);
    
        // Print the values
        std::cout << 
            parsed_structure.a << " " <<
            parsed_structure.b << " " <<
            parsed_structure.c << " " <<
            parsed_structure.d << " " << std::endl;
    
        // Look the size of our structure is different than the size of our buffer due to alignment
        std::cout <<
            "sizeof(buffer)" << "==" << sizeof(buffer) << " " <<
            "sizeof(parsed_structure)" << "==" << sizeof(parsed_structure) << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.