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

  • Home
  • SEARCH
  • 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 8747505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:22:00+00:00 2026-06-13T12:22:00+00:00

I am looking for any library of example parsing a binary msg in C++.

  • 0

I am looking for any library of example parsing a binary msg in C++. Most people asks for reading a binary file, or data received in a socket, but I just have a set of binary messages I need to decode. Somebody mentioned boost::spirit, but I haven’t been able to find a suitable example for my needs.

As an example:
9A690C12E077033811FFDFFEF07F042C1CE0B704381E00B1FEFFF78004A92440

where first 8 bits are a preamble, next 6 bits the msg ID (an integer from 0 to 63), next 212 bits are data, and final 24 bits are a CRC24.

So in this case, msg 26, I have to get this data from the 212 data bits:

  • 4 bits integer value
  • 4 bits integer value
  • A 9 bit float value from 0 to 63.875, where LSB is 0.125
  • 4 bits integer value

EDIT: I need to operate at bit level, so a memcpy is not a good solution, since it copies a number of bytes. To get first 4-bit integer value I should get 2 bits from a byte, and another 2 bits from the next byte, shift each pair and compose. What I am asking for is a more elegant way of extracting the values, because I have about 20 different messages and wanted to reach a common solution to parse them at bit level.

And so on.

Do you know os any library which can easily achieve this?

I also found other Q/A where static_cast is being used. I googled about it, and for each person recommending this approach, there is another one warning about endians. Since I already have my message, I don’t know if such a warning applies to me, or is just for socket communications.

EDIT: boost:dynamic_bitset looks promising. Any help using it?

  • 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-13T12:22:01+00:00Added an answer on June 13, 2026 at 12:22 pm

    If you can’t find a generic library to parse your data, use bitfields to get the data and memcpy() it into an variable of the struct. See the link Bitfields. This will be more streamlined towards your application.

    Don’t forget to pack the structure.

    Example:

    #pragma pack
    
    include "order32.h"
    struct yourfields{
    #if O32_HOST_ORDER == O32_BIG_ENDIAN
       unsigned int preamble:8;
       unsigned int msgid:6;
       unsigned data:212;
       unsigned crc:24;
    #else
       unsigned crc:24;
       unsigned data:212;
       unsigned int msgid:6;
       unsigned int preamble:8;
    #endif
    }/*__attribute__((packed)) for gcc*/;
    

    You can do a little compile time check to assert if your machine uses LITTLE ENDIAN or BIG ENDIAN format. After that define it into a PREPROCESSOR SYMBOL::

    //order32.h
    
    #ifndef ORDER32_H
    #define ORDER32_H
    
    #include <limits.h>
    #include <stdint.h>
    
    #if CHAR_BIT != 8
    #error "unsupported char size"
    #endif
    
    enum
    {
        O32_LITTLE_ENDIAN = 0x03020100ul,
        O32_BIG_ENDIAN = 0x00010203ul,
        O32_PDP_ENDIAN = 0x01000302ul
    };
    
    static const union { unsigned char bytes[4]; uint32_t value; } o32_host_order =
        { { 0, 1, 2, 3 } };
    
    #define O32_HOST_ORDER (o32_host_order.value)
    
    #endif
    

    Thanks to code by Christoph @ here

    Example program for using bitfields and their outputs:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <memory.h>
    using namespace std;
    
    struct bitfields{
      unsigned opcode:5;
      unsigned info:3;
    }__attribute__((packed));
    
    struct bitfields opcodes;
    
    /* info: 3bits; opcode: 5bits;*/
    /* 001 10001  => 0x31*/
    /* 010 10010  => 0x52*/
    
    void set_data(unsigned char data)
    {
      memcpy(&opcodes,&data,sizeof(data));
    }
    
    void print_data()
    {
      cout << opcodes.opcode << ' ' << opcodes.info << endl;
    }
    
    int main(int argc, char *argv[])
    {
      set_data(0x31);
      print_data(); //must print 17 1 on my little-endian machine
      set_data(0x52); 
      print_data(); //must print 18 2
      cout << sizeof(opcodes); //must print 1
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking around here for any reference to a java library for visual
I'm looking for any resources concerning an ASPX file parser and object model. I'm
Looking for a basic AndroidPlot bar graph example. A few people have contacted the
I am looking for solutions BoofCV or any pure java computer vision library that
I am looking for java example solution/library/class for online card game. I'm interested in
I'm looking for a good multiplatform library for vector graphics in C/C++. Any recommendation
I am looking for example uses (or tutorials) of the record library http://www.cs.fit.edu/~pkc/classes/ai/swi-prolog/Manual/record.html for
I've been looking out if there is any java converter library to translate simple
im looking for any information about a builtin algorithm in opengl es to convert
I am looking for any tip or guidance to develop an application like cam

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.