Extracting data similar to the output of xxd from binary files

368 views Asked by At

I have a binary file that represents the output of a sensor.
On my bash-terminal I can show the data of the file using xxd -b atis.es which results in an output like this:

00000000: 01000101 01110110 01100101 01101110 01110100 00100000 Event
00000006: 01010011 01110100 01110010 01100101 01100001 01101101 Stream
0000000c: 00000010 00000000 00000000 00000010 01000000 00000001 ....@.
00000012: 11110000 00000000 00000010 00000000 00000000 11101111 ......

Now I am trying to import this file in my C++ code, but I'm struggling to understand how this works.
The file that I'm experementing on, can be found here with the corresponding explanation of the Byte-Structure. As far as I understand the explanation (I never worked with Binary files before) the first 19 Bytes are just a Header, so the data is written in Byte 20 and following. So in C++ I used this code:

#include <sstream>
#include <fstream>
#include <iostream>
#include <cstdint>

int main(int argc, char **argv){
  std::ifstream data;
  data.open("path-to-file/atis.es", std::ios::binary);

  std::cout<<data.tellg()<<'\n'; % check current position
  data.seekg(20); % jump to 20th byte
  char buffer[100];
  data.read(buffer,100);
  std::cout<<buffer<<std::endl;
}  

From this code I would expect that it outputs 0 first, because tellg() should return 0 as position as long as I didn't do anything with the fsteam, or not? After that I would expect the code to output the first 100 characters after the 20th byte. I'm not sure, if seekg(20) actually jumps to the 20th byte though and I couldn't figure it out reading about it here.
The output I actually get is -1 and some strange triangular character.
Ultimately my target is to store the data of the file in an array or struct with the fields t, x, y, pol. But I guess I can figure that out on my own, as soon as I understood a little bit more about how to use and work with the binary files.
Something that I would like to know additionally is why xxd outputs the header of the file broken down into multiple lines after the 6 Bytes each instead of just putting it before the data.

0

There are 0 answers