CStructSharp
GitHub
Open-source · .NET & WebAssembly

Binary data,
made legible.

CStructSharp reads and writes binary data using a description that looks like a C struct. Give it a layout and some bytes, and it gives you named values. Give it values, and it creates bytes or changes a field in existing data — from C#, Node.js, or JavaScript in a browser, using the exact same layout language.

Zero runtime package dependencies. The core .NET library uses only the .NET runtime, keeping integration simple and your application's dependency tree small.

One layout, two runtimes

Parse the same six bytes twice

The same layout string works on both sides — describe it once, and read it from a .NET process or a Node.js process or browser tab.

Program.cs
using CStructSharp;

var layout = new CStruct("struct header { uint16 kind; uint32 length; };");
byte[] bytes = { 0x02, 0x00, 0x06, 0x00, 0x00, 0x00 };
dynamic header = layout.Parse(bytes.AsSpan(), "header");

Console.WriteLine($"kind = {header.kind}");
Console.WriteLine($"length = {header.length}");
example.mjs
import { parse } from "cstructsharp";

const definition = "struct header { uint16 kind; uint32 length; };";
const bytes = new Uint8Array([0x02, 0x00, 0x06, 0x00, 0x00, 0x00]);

const result = await parse(definition, bytes, { root: "header" });
if (!result.success) throw new Error(result.error.message);
const header = result.data;

console.log(`kind = ${header.kind}`);
console.log(`length = ${header.length}`);
Get started

Install

.NET / C#

Requires a stable .NET 10 SDK. Targets .NET 8 and .NET 10.

dotnet add package CStructSharp

Add it to an existing project, or start a new one with dotnet new console first.

JavaScript / WebAssembly

Node.js 22.14+ and browsers. Includes the WASM runtime; no .NET installation required.

npm install cstructsharp

Save the example above as example.mjs and run node example.mjs, or follow the JavaScript quick start to configure the included Vite plugin for a browser app. The guide also covers the optional standalone browser ZIP.