Click here to Skip to main content
12,551,792 members (51,194 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


42 bookmarked

CBitStream - A simple C++ class for reading and writing variable-length data

, 22 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on a simple C++ class for reading and writing variable-length data.


This article is about a simple C++ class for reading and writing variable-length data streams. This feature is often required when programming compression routines like LZW, or when dealing with binary formats like Flash or PDF. It offers the developer a possibility to write binary chunks of custom size: like 11 bits, or 27 bits, etc. So, basically, output is byte-aligned, but the inner structure can hold different length data chunks.


I found a working solution to this problem here on the CodeProject. But, this one is written in C# .NET, so I could not use it directly. I have not translated the original article, but written this one from scratch. I hope that someone will find this work useful.

Using the Code

Using the class CBitStream is very simple. Please see the code below:

#include <span class="code-string">"BitStream.h"</span>

CBitStream bitStream;
bitStream.WriteData((LPBYTE)"ANSI text...", 12);
bitStream.WriteData((LPWORD)_T("UNICODE text..."), 15);
bitStream.SaveStream(_T("Enter output file path here..."));

Points of Interest

I have found that writing the CBitStream class described in this article was not too difficult as I have thought in first place. Also, now I have a simple tool that can help me in my everyday work.


  • CBitStream class version 1.0.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1091033217-Nov-15 6:34
memberMember 1091033217-Nov-15 6:34 
QuestionAny new/recent version of this class? Pin
junaidfcs6-May-14 10:36
memberjunaidfcs6-May-14 10:36 
QuestionRead in order Pin
Nerexis25-Nov-13 6:22
memberNerexis25-Nov-13 6:22 
GeneralSpice it up with Template functions Pin
rich_malina15-Apr-11 3:07
memberrich_malina15-Apr-11 3:07 
GeneralReadBits Fix Pin
Marco Giuntoni14-Apr-09 0:35
memberMarco Giuntoni14-Apr-09 0:35 
I've tried to use the class for writing and reading short bit fields. While writing with WriteBits works OK reading seems to have an issue.
If I read 3 bits, for example, it keeps them aligned to the left reading them in the higher bits of the DWORD. I changed the function in the following way:
void CBitStream::ReadBits(DWORD& value, long nLen)
// Read single BITs
value = 0x00000000;
long _nLen = max(0, min(32, nLen));
BYTE currentOffset = nLen-1;
BYTE bitValue;
for (long i=0; i<_nLen; i++)
value |= (bitValue << currentOffset);

In this way you always have them with the right weight.
GeneralRe: ReadBits Fix Pin
darkoman14-Apr-09 19:47
memberdarkoman14-Apr-09 19:47 
GeneralNice Pin
BakaBug23-Jan-09 2:00
memberBakaBug23-Jan-09 2:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 22 Jan 2009
Article Copyright 2009 by darkoman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid