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 86083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:11:26+00:00 2026-05-10T22:11:26+00:00

I have a byte stream I need parsed into a struct, and I also

  • 0

I have a byte stream I need parsed into a struct, and I also need to be able to parse the struct back to a byte stream.

Below is an example of what I want where I’ve used BitConverter to parse the values. I hope there is a more efficient way of doing this, because my structs are HUGE!

ref struct TestStruct {     int TestInt;     float TestFloat; };  int main(array<System::String ^> ^args) {     // populating array - just for demo, it's really coming from a file     array<unsigned char>^ arrBytes = gcnew array<unsigned char>(8);     Array::Copy(BitConverter::GetBytes((int)1234), arrBytes, 4);     Array::Copy(BitConverter::GetBytes((float)12.34), 0, arrBytes, 4, 4);      // parsing to struct - I want help     TestStruct^ myStruct = gcnew TestStruct();     myStruct->TestInt = BitConverter::ToInt32(arrBytes, 0);     myStruct->TestFloat = BitConverter::ToSingle(arrBytes, 4);      String^ str = Console::ReadLine();     return 0; } 
  • 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. 2026-05-10T22:11:26+00:00Added an answer on May 10, 2026 at 10:11 pm

    For stuff like this, you usually use a code generator. Let’s assume the source looks like this:

    struct a {     int i; }  struct b {     string name;     struct a a; } 

    What you do is you write a simple parser which searches the source (probably some header file) for ‘struct’, then you read the name of the struct (anything between ‘struct’ and ‘{‘). Write this to the output:

    cout << 'struct ' << name << ' * read_struct_' << name << ' (stream in) {' << NL     << '    struct ' << name << ' * result = malloc (sizeof(struct ' << name << '));' NL parseFields (headerStream); cout << '    return result;' << NL << '}' << NL ; }  

    Note my C++ is a bit rusty so this probably doesn’t compile but you should get the idea.

    In parseFields, you read each line and split it into two parts: Anything before the last space (i.e. ‘int’ in the first example) and the stuff between the last space and and the ‘;’. In this case, that would be ‘i’. You now write to the output:

    cout << 'read_' << fieldType << '(in, &result->' << fieldName << ');' << NL; 

    Note: You’ll need to replace all the spaces ub the field type with ‘_’.

    In the output, this looks like so:

    struct a * read_struct_a (stream in) {    struct a * result = malloc(sizeof(struct a));    read_int(in, &result->i);    return result; } 

    This allows you to define how to read or write an int somewhere else (in a utility module).

    Now, you have code which reads the structure definitions from a header file and creates new code that can read the structure out of some stream. Duplicate this to write the structure to a stream. Compile the generated code and you’re done.

    You will also want to write unit tests to verify that the parsing works correctly 🙂 Just create a structure in memory, use the write methods to save it somewhere and read it back again. The two structures should be identical, now. You will want to write a third code generator to create code to compare two structures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The position/styling of the div can be emulated with: #overlay_div… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer I think this will meet your needs: http://www.codeproject.com/KB/cs/winforms2webforms.aspx This will… May 12, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer You can find a port of Rails' Inflector class at… May 12, 2026 at 12:53 am

Related Questions

I've just installed VS2008 and have run into a problem that I'm sure can
I have an excel document represented as a byte[] and I'm wanting to send
I am implementing the BitTorent protocol using Java via this spec . In the
I have a byte array that I am reading in from a NetworkStream. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.