#c-sharp #decode #deserialize #binary

csharp_binary_encoding

Decode binary data written by C#'s BinaryWriter class

4 releases (2 breaking)

Uses new Rust 2024

new 0.3.0 Apr 6, 2025
0.2.0 Mar 26, 2025
0.1.1 Mar 14, 2025
0.1.0 Mar 14, 2025

#1732 in Encoding

Download history 234/week @ 2025-03-12 2/week @ 2025-03-19 134/week @ 2025-03-26

370 downloads per month

GPL-3.0-or-later

31KB
509 lines

csharp_binary_encoding

A crate for handling binary data in the format used by the C# System.IO.BinaryReader and System.IO.BinaryWriter Classes.

Features

  • f16 Enables function for decoding f16 values. Must be compiled with nightly, since f16 is currently an unstable feature in rust.

Example

Reading values

# use csharp_binary_encoding::{BinaryReader, DataDecodeError};
// &[u8] implements read
let bytes: [u8; 11] = [ 0x8F, 0x72, 0x04, 0x6D, 0x65, 0x6F, 0x77, 0xD7, 0xA3, 0xE8, 0x40 ];

// Construct a reader
let mut reader = BinaryReader::new(bytes.as_slice());

// Read values
assert_eq!(14607, reader.read_7_bit_encoded_int()?);
assert_eq!("meow".to_string(), reader.read_string()?);
assert_eq!(7.27_f32, reader.read_f32()?);
# Ok::<(), DataDecodeError>(())

Writing values

# use csharp_binary_encoding::BinaryWriter;
// Vec<u8> implements write
let mut data: Vec<u8> = Vec::new();

// Construct a writer
let mut writer = BinaryWriter::new(&mut data);

//write values
writer.write_7_bit_encoded_int(14607)?;
writer.write_string("meow")?;
writer.write_f32(7.27_f32)?;

assert_eq!(
    data,
    vec![ 0x8F, 0x72, 0x04, 0x6D, 0x65, 0x6F, 0x77, 0xD7, 0xA3, 0xE8, 0x40 ]
);
# Ok::<(), Box<dyn std::error::Error>>(())

Limitations

  • Currently, only the utf-8 encoding is supported.
  • Developed for and tested with .NET version 9.0. Compatibility with other versions is likely present but should not be counted on.

Dependencies

~225–660KB
~15K SLoC