0


So I have some binary data that I read, process and need to "split" into different variables, like this:

int *buffer;
buffer = malloc(size); 
fread(buffer,size,1,file);
buffer = foo(buffer);

The result looks something like this in my debugger:

01326A18 5F4E8E19 5F0A0000

I want the first byte ( 01 ) to be int a.
The following 4 bytes are the first timestamp b (should be 5F186A32)
The following 4 bytes are the second timestamp c (should be 5F198E4E)
The 0A is supposed to be int d.

My Problem is that I can put the 1 into a, with (*buffer) & 0xff;,
but I'm not able to read the first timestamp correctly since its from second to 5th byte and not align with the int declaration of the buffer. If I print *(buffer +1) it gives me the second int and prints "198E4E5F" It would be better if I were able to target n byte from every position in my data.

thx in advance.

Simon Huenecke
  • 97
  • 1
  • 1
  • 7
  • 1
    I hope that `buffer = foo(buffer);` is just an example because it destroys the memory allocated with`malloc`. – Paul Ogilvie Jul 23 '20 at 13:54
  • This post really needs to show your effort, in`C`. i.e. provide a [mcve], then with that context your problem description may make sense. – ryyker Jul 23 '20 at 13:58
  • @PaulOgilvie `buffer = foo(buffer);` need not destroy the memory. For example, the assignment may be for reflecting `realloc()` done within `foo()`. – MikeCAT Jul 23 '20 at 14:01
  • why not to use struct with right variables inside and start reading whole struct at once? Like fread(some_struct, sizeof(struct), 1, file). – Marcin Jul 23 '20 at 14:02
  • _The following 4 bytes are the first timestamp `b` (should be `5F186A32`)_ What does this even mean? Where is `b` defined? You go from looking at the first byte `01`, then say the next 4 bytes are to be converted to timestamp `b`? Where does `5F186A32` come from? – ryyker Jul 23 '20 at 14:07
  • @PaulOgilvie That line doesn't "destroy memory" – user253751 Jul 23 '20 at 16:10
  • @user253751, you are absolutely right! Only a hammer, lightning or an explosion can destroy memory. (But yes, as Mik Cat says, there might be some hidden functionality there, although I doubt it given the task of splitting the buffer.) – Paul Ogilvie Jul 24 '20 at 09:52

2 Answers2

2

Something like this will work on most little-endian platforms. Just fread the same way.

struct {
  uint8_t a;
  uint32_t timeStamp1;
  uint32_t timeStamp2;
  uint8_t d;
} buffer __attribute__((packed));
assert(sizeof buffer == 10);  /* check packing */
stark
  • 12,615
  • 3
  • 33
  • 50
  • 1
    `__attribute__` is not standard C. And of course, as you allude, even on implementations that have that extension, this approach relies on the endianness of the implementation's `uint32_t` to match that used in the file. – John Bollinger Jul 23 '20 at 14:19
0

If you set your buffer type as char* this will make it point to the 1 byte of chunks. Then if try to get buffer+2, it will return the second byte of the buffer, unlike int pointer which will return the 8th byte of the buffer. Do not forget to update your size in malloc call, since you get your memory with 1 byte chunks in this case. Also this link may be helpful.

Uzun Emre
  • 151
  • 5