vector <bitset <6> > to stringstream

G'day

I'm trying to write an AIS decoder (NMEA 0183) as a project while i learn C++ and have become stuck trying to get a vector containing binary to a stringstream so i can use it to decode the data payload section of the message. The information that needs to be extracted isn't always in 6 bit blocks so i thought a stingstream would be good. I can use the getline option to grab what i need each time as getline has a memory of what i grabbed last time i used it.

Here's a sample of the section of the message I'm currently working on(data payload portion);
13HOI:0P0000VOHLCnHQKwvL05Ip

And here's it's NMEA 6bit binary representation that's currently in the vector in question (each block of 6 is in it's own cell in the vector);
000001 000011 011000 011111 011001 001010 000000 100000 000000 000000 000000 000000 100110 011111 011000 011100 010011 110110 011000 100001 011011 111111 111110 011100 000000 000101 011001 111000

Below is the code i used to get to this point;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  std::string MsgPayload;
    getline(Conv, MsgPayload, ',');

    std::vector <int> Ascii8bit;
    int j (MsgPayload.length());

    {
    for (int i=0; i<j; i++)
        {
        Ascii8bit.push_back(MsgPayload.at(i));
        }
    }

    std::vector <int> Ascii6bit;

    {
    for (int i=0; i<j; i++)
        {
        int a = Ascii8bit[i];
        int b = a - 48;
        if( b >= 41) b = b - 8;
        Ascii6bit.push_back(b);
        }
    }

    std::vector <std::bitset<6> > BitSeq;
    {
        for (int i=0; i<j; i++)
        {
            std::bitset<6> a = (Ascii6bit[i]);
            BitSeq.push_back(a);
        }
    }


Thanks in advace
Last edited on
Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <bitset>
#include <string>
#include <iomanip>

template< std::size_t N > std::string bitsets_to_string( const std::vector< std::bitset<N> >& data )
{
    std::string str_bits ;
    for( auto bs : data ) str_bits += bs.to_string() ;
    return str_bits ;
}

int main()
{
    const std::vector< std::bitset<6> > my_bits =
    {
        0b000001, 0b000011, 0b011000, 0b011111, 0b011001, 0b001010, 0b000000, 0b100000, 0b000000,
        0b000000, 0b000000, 0b000000, 0b100110, 0b011111, 0b011000, 0b011100, 0b010011, 0b110110,
        // more ...
    };

    std::string str_bits = bitsets_to_string(my_bits) ;
    std::cout << std::quoted(str_bits) << "\n\n" ;

    // extract 8-bit segments
    const std::size_t NBITS = 8 ;
    while( str_bits.size() % NBITS ) str_bits.push_back( '0' ) ; // pad to multiple of NBITS
    
    for( std::size_t i = 0 ; i < str_bits.size() ; i += NBITS )
    {
        const std::bitset<NBITS> bs( str_bits, i, NBITS ) ;
        std::cout << bs << ' ' << bs.to_ulong() << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/a3ebffbbbae94f1a
Thanks for the reply JLBorges.

The template at the top requires iomanip and will work without listing the binary under the const declaration? The reason i ask is that i plan for this to take a live feed of AIS data and feed the results into a database, so i won't be able to list the binary.

The NMEA 0183 coding uses a 6bit ASCII code, so I've converted to 6bit binary in the previous steps, so i won't need to convert back to 8 bit. thanks for the code for it, I'm sure it will come in handy in future endevours.
> The template at the top requires iomanip

No. Just <bitset>, <vector> and <string>.


> will work without listing the binary under the const declaration?

Yes. <iostream> and <iomanip> are needed only if we want to perform i/o.
Took me a few minutes to realise that the error getting thrown was because the version of Code Blocks i have doesn't support c++14. I'll update mingw when i get home.

Thanks again for your help JLBorges

Edit: If you don't have c++14 support, just remove the std::cout line. This will also remove the white spaces in the string.
Last edited on
Topic archived. No new replies allowed.