© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
24 replies
RJ

❔ Runtime Endianness for Reading Structured Binary Data

Is there a standard way to deal with reading structured binary data in from a stream where the endianness is known only at runtime? In other words, it would be great if there was something with the same API as the
BinaryReader
BinaryReader
but where the endianness for the reader could be set.

I'm pretty new to C# so I didn't know if there was a idiomatic way to do this. In go I have used the
binary.Read
binary.Read
(https://pkg.go.dev/encoding/binary#Read) API to allow for reading in primitive types while specifying the endianness. Similarly, I have used the Java
ByteBuffer
ByteBuffer
type (https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html) to accomplish this on the JVM.

From my research, I have discovered (correct me if I'm wrong) that everything in dotnet is little endian. I have also found the
BinaryPrimatives
BinaryPrimatives
class which offers different methods for reading/writing binary primitives with different endiannesses. However, using this would require conditional statements wherever a read is performed. I wasn't sure if there was an extant class that provides a sleeker API for this. I know I could wrap it myself, but didn't want to reinvent the wheel if it was just something I missed
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Deserializing binary data
C#CC# / help
3y ago
❔ Understanding reading values from binary
C#CC# / help
3y ago
Data Reading Suggestion
C#CC# / help
4y ago
❔ ✅ Issue with data reading from database
C#CC# / help
3y ago