bit-packing

Managing bit packed data using C#

I'm working on a TCP based application that processes bitpacked messages, meaning: The messages transmitted/received are not byte aligned. For instance 3 bits represent field 1, where 19 bits may represent field 2. My question is, does anyone know of a C# library that can take a set of bytes and set/get an arbitrary range of bits with...

What is a better method for packing 4 bytes into 3 than this?

I have an array of values all well within the range 0 - 63, and decided I could pack every 4 bytes into 3 because the values only require 6 bits and I could use the extra 2bits to store the first 2 bits of the next value and so on. Having never done this before I used the switch statement and a nextbit variable (a state machine like dev...

C++ Data Member Alignment and Array Packing

During a code review I've come across some code that defines a simple structure as follows: class foo { unsigned char a; unsigned char b; unsigned char c; } Elsewhere, an array of these objects is defined: foo listOfFoos[SOME_NUM]; Later, the structures are raw-copied into a buffer: memcpy(pBuff,listOfFoos,3*SOME_NUM); ...

Client-Server Data Encryption and Protocol Design

Hello, I'm writing a client-server application to be used in a computer lab and act as a service (without running as a service). I have a console application calling the native function "ShowWindow"/SW_HIDE using the console's HWND object -- this gives it what I am wanting here. The server/client is working, I've sent the message "Hello...

C# equivalent of python's struct.pack

Is there a library for C# that allows similar functionality to python's struct from the standard library? One can emulate the struct library quite closely with real aligned structs. But I didn't find yet any way to directly control the endianess in C#'s structs (the C#'s structs seems to be geared more towards COM interop, and less towa...

Bit packing of array of integers

I have an array of integers, lets assume they are of type int64_t. Now, I know that only every first n bits of every integer are meaningful (that is, I know that they are limited by some bounds). What is the most efficient way to convert the array in the way that all unnecessary space is removed (i.e. I have the first integer at a[0], ...

How to pack the required bits of a structure in a char* ?

Language : C++ I am working on Bit Packing (Extracting the required bits from the given data and packing them in a char*) . My code currently supports : - Integers - Characters - Strings Now if I have to store the required bits of a structure, how should I go about it ? I mean what should I expect as input parameters for a generaliz...

What is VC++ doing when packing bitfields?

To clarify my question, let's start off with an example program: #include <stdio.h> #pragma pack(push,1) struct cc { unsigned int a : 3; unsigned int b : 16; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; ...