0

I have a C structure that contains several int data types; I shift and mask the data types in this structure to produce 73 bits of ordered data. I need to write many lines of these 73 bits into a txt file in format "0x73bits" using fprintf.

It must be exactly 73 bits of data every time in just a few lines of code. I can do this using 64 bits then playing around with 9 bits. But it is messy.

Can anyone suggest a more clean professional method?

jon jon
  • 55
  • 1
  • 3
  • 4
    Ehm, "exactly 73 bits" cannot be written in the 0xNNN format. – Mr Lister Oct 31 '16 at 07:03
  • i want to write "0x" then 73 bits of data – jon jon Oct 31 '16 at 07:06
  • 1
    Can you provide an example of exactly what it is you want to write? E.G. if all the bits are 1, what is the output supposed to be? – Mr Lister Oct 31 '16 at 07:15
  • 1
    Seriously. Update your question with an example output of these "73bits", especially if using what appears to be hexadecimal characters. Given an *single* hex character represents one *nibble* (four bits), you're going to be hard-pressed to output anything that is not at least a multiple of 4 (which the prime 73 is clearly *not*). – WhozCraig Oct 31 '16 at 07:17
  • all bits set to one - 0x1fffffffff – jon jon Oct 31 '16 at 07:29
  • yes i know that - that is the point its a nonstandard data type – jon jon Oct 31 '16 at 07:30
  • OK, so you're OK with writing 19 hex digits? That wasn't clear from the question. – Mr Lister Oct 31 '16 at 07:35
  • I think I understand. If that is the case, then the lead char (post `0x`) will always be `1` or `0`, and the remaining bits are sprinkled throughout nine complete nibbles. Is that right? (and btw, that example should be *in your question*; not down here in a stack of comments). – WhozCraig Oct 31 '16 at 07:38
  • 2
    It is entirely unclear what you are asking. In the file supposed to be binary or text? Are you supposed to write text representation of bits, or binary data? In case of the latter, what file system are you using that allows files to have size in bits and not bytes? What does this have to do with embedded systems? – Lundin Oct 31 '16 at 07:49
  • yes, the bits that get set will depend entirely upon the data. Each of the ints in the structure correspond to certain bits in the 73bit word. eg the final int in the structure is used to set/clear the MSB in the 73 bit word. – jon jon Oct 31 '16 at 07:49
  • Why don't you post some actual code, showing how the data is stored? Presumably a `uint8_t[10]`? So, your actual question then becomes: "how do I convert a byte array into a hex string in C", and that's a [duplicate](http://stackoverflow.com/q/6357031/69809). – vgru Oct 31 '16 at 08:40
  • Just use fwrite and fread on binary files, don't write 0x then 73 bits (impossible) - just write the bits themselves (calculate what bytes come out of the bits using bit shifts) and when reading stop every 73 bits – Zach P Oct 31 '16 at 08:44
  • It is not possible to printf a custom data type with a single conversion (single % character). You need to write a custom function for that. – n. m. could be an AI Oct 31 '16 at 09:22
  • The least you can do is show the "several int data types" themselves. – Mr Lister Nov 01 '16 at 07:13
  • It is neither 73 bits of memory nor is it 73 bits on disk. It would need to be in units of bytes. So 10 bytes minimum, depending on how you are using this "structure" (bitfields for example) no reason to assume it is 10 bytes could be more, many many more (in ram). Then there is the what format do you want these on disk. 0x1FFFFFFFFFFFFFFFFFF can take as little as 10 bytes but could also take 20 bytes or more if you want a carriage return or other formatting. And of course it will consume a whole sector on disk so 512, 1024, ...16384...minimum bytes on disk. What exactly are you after? – old_timer Nov 02 '16 at 03:51

1 Answers1

2

You can not write bits to a file ,only bytes. You will have to leave empty bits at the end of the file.Also in memory your structure is represented in bytes not bits.

You can convert your bits data into a string, this will waste disk space but might be what you want. here is an example of printing a binary string:printf binary

This is an example of fprintf to a file:fprintf to a file

Community
  • 1
  • 1
O_Z
  • 1,515
  • 9
  • 11