<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'binary files in c/c++' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'binary files in c/c++' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 18:26:35 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 18:26:35 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/376682/binary-files-in-cc++/</link>
      <description>hi! i need a solution to this problem concerning writing binary data to files. that is, i would like to know how to write individual bits into a file, without them being written as individual characters.....&lt;br /&gt;
&lt;br /&gt;
for eg, if we write the following array into a file :-&lt;br /&gt;
&lt;br /&gt;
char * str = "10010101101010110101";&lt;br /&gt;
&lt;br /&gt;
it will be a 20 &lt;strong&gt;byte &lt;/strong&gt;file. but i need to write those as bits, so that the file would be a 20 &lt;strong&gt;bit &lt;/strong&gt;file.....ie, 2 and a half bytes......&lt;br /&gt;
any suggestions??&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/376682/binary-files-in-cc++/</guid>
      <pubDate>Wed, 01 Oct 2008 12:31:10 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/376683/re-binary-files-in-cc++/#376683</link>
      <description>: hi! i need a solution to this problem concerning writing binary data &lt;br /&gt;
: to files. that is, i would like to know how to write individual bits &lt;br /&gt;
: into a file, without them being written as individual characters.....&lt;br /&gt;
: &lt;br /&gt;
: for eg, if we write the following array into a file :-&lt;br /&gt;
: &lt;br /&gt;
: char * str = "10010101101010110101";&lt;br /&gt;
: &lt;br /&gt;
: it will be a 20 &lt;strong&gt;byte &lt;/strong&gt;file. but i need to write those as bits, &lt;br /&gt;
: so that the file would be a 20 &lt;strong&gt;bit &lt;/strong&gt;file.....ie, 2 and a half &lt;br /&gt;
: bytes......&lt;br /&gt;
: any suggestions??&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
Use strtol with base 2 to convert a binary string to a long integer. Then you can cast it to a char (or whatever datatype/size you want) and write it to the file at the correct position.&lt;br /&gt;
Best Regards,&lt;br /&gt;
Richard&lt;br /&gt;
&lt;br /&gt;
The way I see it... Well, it's all pretty blurry</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/376683/re-binary-files-in-cc++/#376683</guid>
      <pubDate>Wed, 01 Oct 2008 12:36:44 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/376684/re-binary-files-in-cc++/#376684</link>
      <description>: : hi! i need a solution to this problem concerning writing binary data &lt;br /&gt;
: : to files. that is, i would like to know how to write individual bits &lt;br /&gt;
: : into a file, without them being written as individual characters.....&lt;br /&gt;
: : &lt;br /&gt;
: : for eg, if we write the following array into a file :-&lt;br /&gt;
: : &lt;br /&gt;
: : char * str = "10010101101010110101";&lt;br /&gt;
: : &lt;br /&gt;
: : it will be a 20 &lt;strong&gt;byte &lt;/strong&gt;file. but i need to write those as bits, &lt;br /&gt;
: : so that the file would be a 20 &lt;strong&gt;bit &lt;/strong&gt;file.....ie, 2 and a half &lt;br /&gt;
: : bytes......&lt;br /&gt;
: : any suggestions??&lt;br /&gt;
: : &lt;br /&gt;
: &lt;br /&gt;
: Use strtol with base 2 to convert a binary string to a long integer. &lt;br /&gt;
: Then you can cast it to a char (or whatever datatype/size you want) &lt;br /&gt;
: and write it to the file at the correct position.&lt;br /&gt;
: Best Regards,&lt;br /&gt;
: Richard&lt;br /&gt;
: &lt;br /&gt;
: The way I see it... Well, it's all pretty blurry&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
thanks for your suggestion... you mean, first converting the number using strtol from binary to decimal? hmm....might be...i'll try that, but is there any function which will allow me to write individual bits ?&lt;br /&gt;
thanks in advance&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/376684/re-binary-files-in-cc++/#376684</guid>
      <pubDate>Wed, 01 Oct 2008 12:43:47 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/376731/re-binary-files-in-cc++/#376731</link>
      <description>: thanks for your suggestion... you mean, first converting the number &lt;br /&gt;
: using strtol from binary to decimal? hmm....might be...i'll try &lt;br /&gt;
: that, but is there any function which will allow me to write &lt;br /&gt;
: individual bits ?&lt;br /&gt;
: thanks in advance&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
A byte is the smallest unit you can write on (most?) x86 platforms.&lt;br /&gt;
If you want to write bits, the idea is to read the bytes in which the bits reside and then use binary operations on the bytes to set the bits and then write them back to file.&lt;br /&gt;
There's a catch though, and a pretty nasty one too. Bit order is often written to file and stored in memory in a 'mangled' way. And to top it off, I don't think there is óne standard that dictates how this is done. Actually, because of this, you as a programmer don't know how the actual bits are stored. There's no way to find out either, because you're always looking at it byte-wise (or bigger). If at the time they created the CPU they went a bit funny and felt it would be cool to store the bits in a byte 01110010b first all the even ones and then the odd ones, thus 01011100b, then that would still work just as well as what is used now. And the point is, you wouldn't know about it.&lt;br /&gt;
So actually you're question of modifying bits needs another question: How will I assign a number to each bit? Which one will be the first, which the second... what's the 54th bit?&lt;br /&gt;
&lt;br /&gt;
Best Regards,&lt;br /&gt;
Richard&lt;br /&gt;
&lt;br /&gt;
The way I see it... Well, it's all pretty blurry</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/376731/re-binary-files-in-cc++/#376731</guid>
      <pubDate>Thu, 02 Oct 2008 03:06:35 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/376781/re-binary-files-in-cc++/#376781</link>
      <description>: A byte is the smallest unit you can write on (most?) x86 platforms.&lt;br /&gt;
&lt;br /&gt;
Most CPUs work on byte level. If a CPU has an instruction that allows you to work on bit level, it is the task of the compiler to make sure that this particular CPU instruction is used.&lt;br /&gt;
&lt;br /&gt;
In plain English, this means that an assembler programmer might have to adjust their code to this, but never a C/C++ programmer. C/C++ always work on a byte level, and if the CPU happens to have bit instructions, the compiler will translate your byte-wise instructions to bit-wise OP-codes.&lt;br /&gt;
&lt;br /&gt;
: If you want to write bits, the idea is to read the bytes in which &lt;br /&gt;
: the bits reside and then use binary operations on the bytes to set &lt;br /&gt;
: the bits and then write them back to file.&lt;br /&gt;
: There's a catch though, and a pretty nasty one too. Bit order is &lt;br /&gt;
: often written to file and stored in memory in a 'mangled' way. And &lt;br /&gt;
: to top it off, I don't think there is óne standard that dictates how &lt;br /&gt;
: this is done. Actually, because of this, you as a programmer don't &lt;br /&gt;
: know how the actual bits are stored. There's no way to find out &lt;br /&gt;
: either, because you're always looking at it byte-wise (or bigger). &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is very true and the reason why you should avoid C/C++ "bit fields" like the plauge. If you use the bitwise instructions instead, you usually shouldn't need to worry about how the bits are stored in memory: the compiler will handle that for you. Some issues may rise when you port code from a little endian CPU to a big endian one, but in most cases you don't have to worry about this, as long as you use the C/C++ bitwise operators. If you use bit fields, you unleash hell.&lt;br /&gt;
&lt;br /&gt;
: How will I assign a number to each bit? Which one will be &lt;br /&gt;
: the first, which the second... what's the 54th bit?&lt;br /&gt;
&lt;br /&gt;
0x01 is always the first one if you program in C/C++. The only danger is when you are swapping the bytes of a large integer. Then you need to be aware of big/little endian.&lt;br /&gt;
&lt;br /&gt;
Big endian (Motorola/Freescale, Unix, Renesas, Old macs etc):&lt;br /&gt;
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0&lt;br /&gt;
&lt;br /&gt;
Little endian (Intel, Pic, Atmel, New macs etc):&lt;br /&gt;
7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8&lt;br /&gt;
&lt;br /&gt;
However, whenever you use shift &amp;lt;&amp;lt; &amp;gt;&amp;gt;, bitwise &amp;amp; | ^ etc the compiler will handle everything for you. If you for example type  (my_int &amp;amp; 0x0001) on a little endian machine, the compiler will still give you bit 0 as expected and not bit 8. Very convenient.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/376781/re-binary-files-in-cc++/#376781</guid>
      <pubDate>Thu, 02 Oct 2008 07:43:43 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: binary files in c/c++</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/376682/379994/re-binary-files-in-cc++/#379994</link>
      <description>&lt;br /&gt;
: This is very true and the reason why you should avoid C/C++ "bit &lt;br /&gt;
: fields" like the plauge. If you use the bitwise instructions &lt;br /&gt;
: instead, you usually shouldn't need to worry about how the bits are &lt;br /&gt;
: stored in memory: the compiler will handle that for you. Some issues &lt;br /&gt;
: may rise when you port code from a little endian CPU to a big endian &lt;br /&gt;
: one, but in most cases you don't have to worry about this, as long &lt;br /&gt;
: as you use the C/C++ bitwise operators. If you use bit fields, you &lt;br /&gt;
: unleash hell.&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
okay, so i have to stay clear of the bits...hmm...now there is one more doubt; we know that &lt;strong&gt;huffman encoding&lt;/strong&gt; assigns variable length codes to symbols...ie, &lt;strong&gt;not all symbols have an 8-bit code&lt;/strong&gt;.now, if all symbols are still written as a byte to the file, &lt;strong&gt;how is compression achieved???&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
thanks in advance..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/376682/379994/re-binary-files-in-cc++/#379994</guid>
      <pubDate>Sat, 11 Oct 2008 21:18:52 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>