-1

I am new to C++ and wanted to know if a .bin file can be converted to an ASCII file (ASCII file is to be loaded onto EXCEL for further data analysis). The binary log file contains a bunch of messages (BinarayMessage1, BinarayMessage2...) in the form of structures. One such example is as follows:

 typedef struct
 {
   SHeader header; //8 bytes, SHeader is a structure defining the message header
   signed short array1[size]; //256 bytes
   signed short array2[size]; //256 bytes
   float f; //4 bytes
   // a bunch of unsigned short variables of 2 bytes each
   unsigned short m1
   unsigned short m2
   unsigned short m3
   unsigned short m4
   unsigned short m5
   unsigned short m6
   unsigned short m7
   unsigned short m8
} SBinaryMessage69 //Total length is 540 bytes

Each message has a different structure. The binary file does not have any encoding. Is there a way in C++ to convert all the binary data in the .bin file to an ASCII .log file (to be opened in EXCEL later for some analysis- just for information, not included in the scope of question)?

(P.S. I am new to stackoverflow as well, so, sorry if my way of asking the question is wrong!)

  • 1
    I don't understand the question. Ok, so you know the details of the binary format. Now you need to write code to convert it to a textual format. What do you want from us? – HolyBlackCat May 08 '22 at 21:01
  • 2
    At a minimum, you'll need to write some code to parse the data from binary file. SUGGESTION: perhaps you can write your "text" in [.csv](https://en.wikipedia.org/wiki/Comma-separated_values) format. Double-clicking a .csv file will usually open the file directly in Excel. – paulsm4 May 08 '22 at 21:02
  • @HolyBlackCat, I was hoping to get some coding examples for parsing the data from the binary file to text. I tried searching for references but couldn't find any for an entire binary file. – spinelli_97 May 08 '22 at 21:15
  • hacked together in 2 seconds: `SBinaryMessage69 msg; ssize_t len = read(fd, &msg, sizeof(msg)); assert(len == sizeof(msg));` and then write a print function to output it as text. – Goswin von Brederlow May 08 '22 at 21:17
  • @paulsm4, thank you for editing my question, I was figuring out how to do that. Do you happen to know any examples/references for parsing the data from the binary file to text? – spinelli_97 May 08 '22 at 21:17
  • The first step is to figure out how you're going to read the file: `fopen` and friends or `std::ifstream`. The former is probably simpler. Then you read the structs as arrays of bytes (using [`fread`](https://en.cppreference.com/w/c/io/fread), assuming you used `fopen`). The only problem is to determine which struct to read next, but that's something only you know. – HolyBlackCat May 08 '22 at 21:19
  • @GoswinvonBrederlow, I just know the format of the content in the binary file. the structure I mentioned is in there somewhere in the binary. I cannot access it directly. – spinelli_97 May 08 '22 at 21:24
  • Then you better find out what the structure is. Likely the header of each message will have a length so you can at least split the log into messages even if you don't know the structure of each message type. – Goswin von Brederlow May 08 '22 at 21:26
  • 1
    @spinelli_97: before you start writing any "code", your *FIRST* step to decide on an "output format". Perhaps csv will work ... perhaps not. SUGGESTION: Create a test .csv file in notepad. Try typing a few lines of "real data". Save the "test file" you created manually, and see if it opens in Excel and displays the way you need it to. (I assume you already have some test data to work with, correct?) – paulsm4 May 08 '22 at 21:26
  • @HolyBlackCat, so if I know details like the exact size of the structures and what's in the header for instance, can I search for that particular structure and read it? – spinelli_97 May 08 '22 at 21:31
  • @paulsm4, my job is to convert the .bin file to an ASCII .log file which can be loaded onto Excel in proper format using tabs as delimiters I'm told. Unfortunately, I don't have real data anywhere, I just have a binary file. – spinelli_97 May 08 '22 at 21:45
  • Are you able to read the binary file? If you had the data, could you write it to an ASCII file? If you had an ASCII file, could you load it into Excel? Asking about all the steps at once is too broad. Divide your task into smaller pieces, then do that again. And again. Keep going until you are actually stuck, then ask about that specific step. – JaMiT May 09 '22 at 00:14
  • I do that all the time, `xxd -g 1 file.bin | vi -` – Eljay May 09 '22 at 01:23
  • @Eljay, is there a way I can use that command on a Windows system? – spinelli_97 May 09 '22 at 04:50
  • Forget the command, it converts binary to hex. *"can I search for that particular structure"* You can set the offset within the file you want to read from (`fseek`), then read an array of bytes from that offset. That's all the tool you have. – HolyBlackCat May 09 '22 at 07:02
  • Yes, you can use WSL, or Cygwin, or GitBash. I've had good luck with Cygwin. – Eljay May 09 '22 at 11:50
  • @spinelli_97: the suggestion about [xxd](https://linux.die.net/man/1/xxd) is probably USELESS for your purposes. You want to PARSE the CONTENTS of the file. "xxd" merely converts the the file into a string of hexidecimal digit pairs. – paulsm4 May 09 '22 at 15:07
  • @HolyBlackCat, what logic should I use to do the actual conversion from binary to Ascii? I used .readlines() function in Python to just see how the file looks like, and the data looked like '\x00\x03\x9A....' – spinelli_97 May 09 '22 at 18:13
  • If you extract just the strings, they don't need any conersion. It's the additional non-text data that looks weird. – HolyBlackCat May 09 '22 at 18:57
  • @HolyBlackCat, do you think the answer by slaphappy in https://stackoverflow.com/questions/26845538/parsing-a-binary-file-what-is-a-modern-way would work in my case? – spinelli_97 May 10 '22 at 19:54
  • It could work, try it. – HolyBlackCat May 10 '22 at 19:55
  • @HolyBlackCat, turns out that method could work, but only problem is that the binary data contains many such structures. One way of identifying the structure is by its header. Could you give me an example of logic which will identify the header and pass it to the structure only if the header matches? – spinelli_97 May 10 '22 at 22:42
  • @GoswinvonBrederlow, I have the header size and contents for every message in the binary file. How do I look for a specific header? For instance, the header for the message I want is 0x4E494224 0x02100074 (in hex) and its size is 8 bytes. All the messages have a similar header, with 0x4E494224 being common, only the 2nd part changes. – spinelli_97 May 11 '22 at 01:07
  • @spinelli_97 Could be the first 4 bytes are a magic to mark the header and the second 4 bytes are from an enum of types. Or maybe the size if stored as the 5th byte as multiple of 4? You have too look at far more than one header to reverse engineer stuff like that. But if you know every header starts with 0x4E494224 why not simply use that to split the data into messages? – Goswin von Brederlow May 11 '22 at 10:54
  • @GoswinvonBrederlow, could you show me the code/pseudocode to split the messages according to the header 0x4E494224? And how do I keep track of different messages and their sizes? I want to ultimately look for the header 0x4E494224 0x02100074 and then read the following data (532 bytes) into the structure I mentioned in my question.. – spinelli_97 May 11 '22 at 19:37

0 Answers0