-2

Lets say I have 2 variables:

int var1 = 1; //1 byte
int var2 = 2; //1 byte

I want to combine these and encode as a 32bit unsigned integer (uint32_t). By combining them, it would be 2 bytes. I'd then fill the remaining space with 2 bytes of 0 padding. This is to write to a file, hence the need for this specific type of encoding.

So by combining the above example variables, the output I need is:

1200 //4 bytes
Alex
  • 49
  • 7
  • The output of what code, exactly? Do you want to output a string, or an int? In other words: show us the code you have so far, if possible as a [mcve]. – Rudy Velthuis Apr 05 '17 at 09:35
  • 1
    Are you saying that you want to shift the first value up 24 bits and the second value up 16 bits? Even if that's what your asking, the displayed value would not be 1200 unless you are showing the hex value... – David Hoelzer Apr 05 '17 at 09:35
  • What will the maximum (*and* minimum) values of e.g. `var1` and `var2` be? Will they always be in the range of a signed byte? – Some programmer dude Apr 05 '17 at 09:35
  • 3
    an `int` is not 1 byte on most systems ... – Mathieu Borderé Apr 05 '17 at 09:36
  • Well it is to write a file. So I'm not sure if I should 1.) write var1 var2 0 0, or 2) I should combine them first and then write to the file. – Alex Apr 05 '17 at 09:36
  • And `1200` is not 4 bytes either. Is that an int, and you want the value 1000 + 200 (decimal), or a string with 4 characters, or hex 1200 or what exactly? Your question is not clear. – Rudy Velthuis Apr 05 '17 at 09:38
  • Combine how exactly? As string of x characters to a text file, or as bytes, combined in one way or another, to a binary file, or what? Please clarify. – Rudy Velthuis Apr 05 '17 at 09:39
  • 1
    In other words: try to realize what exactly you want to achieve. In that process, you may find an answer yourself. Most programming is exactly specifying what you want to do and then translating that into code. – Rudy Velthuis Apr 05 '17 at 09:41
  • You need to clarify for us the exact file format specifications – bolov Apr 05 '17 at 09:41
  • Not as a string of characters. I need to store data blocks in a binary file like so 'var1 var2 0 0'. I also need to be able to retrieve var1 & var2. So, I'm wondering what the best way to get this into a binary file is? – Alex Apr 05 '17 at 09:41
  • 1
    answer me this: do you think that the number 12345 (twelve thousands three hundred forty five) has 5 bytes? – bolov Apr 05 '17 at 09:43
  • What do you mean with "a data block like `var1 var2 0 0`"? note that ints like var1 and var2 can have values well beyond 255, so how do you handle that? If you "combine" them, is the result little or big endian? – Rudy Velthuis Apr 05 '17 at 09:44
  • @RudyVelthuis sorry i should have said that the max value is 255 – Alex Apr 05 '17 at 09:45
  • 1
    Then write them as bytes. Or you can "combine" them by shifting the first value left by 24 and the second by 16 and then or-ing the results into a int, and then write that full int. – Rudy Velthuis Apr 05 '17 at 09:46

1 Answers1

2

There's no need to go the roundabout way of "combining" the values into an uint32_t. Binary files are streams of bytes, so writing single bytes is very possible:

FILE * const out = fopen("myfile.bin", "wb");
const int val1 = 1;
const int val2 = 2;
if(out != NULL)
{
  fputc(val1, out);
  fputc(val2, out);
  // Pad the file to four bytes, as originally requested. Not needed, though.
  fputc(0, out);
  fputc(0, out);
  fclose(out);
}

This uses fputc() to write single bytes to the file. It takes an integer argument for the value to write, but treats it as unsigned char internally, which is essentially "a byte".

Reading back would be just as simple, using e.g. fgetc() to read out the two values, and of course checking for failure. You should check these writes too, I omitted it because error handling.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • There should be a couple of fputc(0, out); before the fclose(out); to completely answer the question (" I'd then fill the remaining space with 2 bytes of 0 padding.") – Neil Apr 05 '17 at 10:21
  • Note that an `int` can be >255, and if you `fputc(256)` it will write `0`. @Alex - If your variables are meant to hold bytes, declare them as `byte`. – slim Apr 05 '17 at 10:22
  • @Neil True, that's pointless but what the OP wanted. I added them, thanks. – unwind Apr 05 '17 at 11:23