Archive for July, 2010

Encode / Decode n-bit Integers into byte[]

A pain to write, and my google-fu was not strong enough to find someone who’d done it for me.

The class allows you to encode / decode 4, 9 or 17 bit (for example) integers to and from a byte[]. I needed it to store some binary keys in a database that are made up of multiple keys, in my case the difference between a byte[] storing 9 bit integers and one storing 16/32 bit integers would be very costly. If you are doing something similar in MSSQL watch out for 0 byte right truncation (reserve the value of 0 perhaps). This version of the code supports 1 though 31 bit unsigned integers, it could easily be extended beyond this if needed.

Code available here on code project (click on tips).


Read Full Post »