Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active November 27, 2021 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulicuu/ca7bb262e076511434eabe876b900d1b to your computer and use it in GitHub Desktop.
Save kulicuu/ca7bb262e076511434eabe876b900d1b to your computer and use it in GitHub Desktop.
Byte parsing in C/C++
0000 0867 4e58 2d30 3100 4002 6666 6666
6666 0000 025e 4b4f 2d52 4700 407b 8000
0000 0000 0000 2328 4246 2d47 4700 4060
b666 6666 6666 0000 07d1 544d 2d41 3100
3ff0 0000 0000 0000 0000 07c7 4c49 2d4e
5800 3ff6 6666 6666 6666 0000 0000 4153
2d44 4600 401f 1c71 c1e4 3b7e 0000 07c0
4249 2d47 4200 4014 0000 0000 0000 0000
002a 4c49 2d46 4500 4009 20c4 9ba5 e354
0000 0063 4245 2d4e 4400 3fdc cccc cccc
cccd 0000 0019 5a41 2d50 5000 0000 0000
0000 0000 0000 ffff 4241 2d5a 4c00 4040
56c8 b439 5810 0000 000c 414e 2d54 4900
3fb9 9999 9999 999a 0000 000b 4241 2d43
4b00 3fc9 9999 9999 999a 0000 000a 5741
2d52 4400 3fd3 3333 3333 3333
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <filesystem>
#include <fstream>
#include <string>
#include <cstdio>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;
using namespace std::filesystem;
size_t stat3 (std::string filepath) {
struct stat buf;
if (stat(filepath.c_str(), &buf) == -1 ) {
std::cout << "stat failed" << std::endl;
} else {
return buf.st_size;
}
}
typedef union
{
float number;
uint8_t bytes[8];
} FLOATUNION_t;
typedef unsigned long uint32;
uint32 deserialize_uint32(unsigned char *buffer)
{
uint32 value = 0;
value |= buffer[0] << 24;
value |= buffer[1] << 16;
value |= buffer[2] << 8;
value |= buffer[3];
return value;
}
int main () {
std::string filepath = "../../../../../cpp\\cpptest01.bin";
size_t size = stat3(filepath);
vector<unsigned char> bytes2(size, 0);
ifstream infile(filepath.c_str(), ios::in | ios::binary);
infile.read((char*)&bytes2[0], bytes2.size());
unsigned char buff_1[4];
unsigned char buff_2[6];
unsigned char buff_3[8];
for (int m = 0; m < size; m++) {
if ((m == 0) || (m % 18 == 0)) {
// parse and print results
for (int i = 0; i < 4; i++) {
buff_1[i] = bytes2[m + i];
}
for (int i = 0; i < 6; i++) {
buff_2[i] = bytes2[i + 4 + m];
}
for (int i = 0; i < 8; i++) {
buff_3[i] = bytes2[i + 10 + m];
}
FLOATUNION_t myFloat;
for (int i = 0; i < 8; i++) {
myFloat.bytes[i] = buff_3[i];
}
cout << deserialize_uint32(buff_1) << endl;
cout << buff_2 << endl;
cout << myFloat.number << endl;
cout << endl;
}
}
return 0;
}

You have been given a data file containing a list of known non-local metaparticles.

The format of the data file is an array of interleaved structures representing the following data for each record:

  • a 32-bit unsigned integer (4 bytes)
  • 6 bytes of ASCII string, null-terminated
  • a 64-bit floating point number (8 bytes)

All scalar values are encoded big-endian.

Write a program to read the data and print each record with each field on its own line, and a blank line after each record. For example, two records might look like:

555
AX-88
6.35

885
OK-01
3.141

Your program must print all of the records in the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment