PythonStructCS 1.0.1

dotnet add package PythonStructCS --version 1.0.1
                    
NuGet\Install-Package PythonStructCS -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PythonStructCS" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PythonStructCS" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="PythonStructCS" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PythonStructCS --version 1.0.1
                    
#r "nuget: PythonStructCS, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PythonStructCS@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PythonStructCS&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=PythonStructCS&version=1.0.1
                    
Install as a Cake Tool

PythonStructCS

A quick port of the essential features of python's struct library to C#

PyStruct.pack(string format, List<object> data) -- returns a byte array (byte[]) with the packed data PyStruct.unpack(string format, byte[] data) -- returns an object List (List<object>) with the unpacked data PyStruct.calcSize(string format) -- returns the byte size of the given format

This library is not meant to be exactly the same as the original library, nor is it even meant to maintain 100% compatibility, its a single "good enough" class for my personal use.

Formats

A format should be a string starting with the endian of the data, followed by specifier characters, prefixed with their count (or size), for example:

<32s4i

is interpreted as little endian (Least significant Byte first), a 32 byte long string (as strings are variable length), and 4 integers (as integers are all 4 bytes)

Endianness

In the original Python struct library there were 5 supported endian characters:

  • @ - Native Endian with native sizing
  • = - Native Endian with standard sizing
  • < - Little Endian with standard sizing
  • > - Big Endian with standard sizing
  • ! - Network (Big Endian with standard sizing)

only < and > are actually implemented in this library:

@, and = are aliases for <, and ! is an alias for >

this should produce the same results in the vast majority of cases.

Supported format specifiers

the majority of this documentation is copied straight from the previously linked python page

1 byte

  • x - Pad Byte
  • c - Character (Char)
  • b - Signed Byte (SByte) (signed character in python)
  • B - Unsigned Byte (Byte) (unsigned character in python)
  • ? - Boolean
  • e - Half

2 bytes

  • h - short (Int16)
  • H - unsigned short (UInt16)

4 bytes

  • i - Integer (Int32)
  • I - Unsigned Integer (UInt32)
  • l - Long (Int32)
  • L - Unsigned Long (UInt32)
  • f - Float (Single)
  • d - Double

8 bytes

  • q - Long Long (Int64)
  • Q - Unsigned Long Long (UInt64)

Variable

  • s (String)

(Currently) Unsupported format specifiers

  • n ssize_t
  • N size_t
  • p char[] (Pascal string)
  • P void*
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 339 3/23/2025
1.0.0 311 3/23/2025