Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:41:19+00:00 2026-06-12T15:41:19+00:00

I have a problem considering the endianness of my data types. I have to

  • 0

I have a problem considering the endianness of my data types. I have to send data over the ethernet using TCP / IP. The byte order however needs to be big endian when being sent and is in big endian, when being received. Therefore I try to reverse all my date before sending it using this class:

class ReverseBinaryReader : BinaryReader
{
    private byte[] a16 = new byte[2];
    private byte[] ua16 = new byte[2];
    private byte[] a32 = new byte[4];
    private byte[] a64 = new byte[8];
    private byte[] reverse8 = new byte[8];
    public ReverseBinaryReader(System.IO.Stream stream) : base(stream) { }

    public override int ReadInt32()
    {
        a32 = base.ReadBytes(4);
        Array.Reverse(a32);
        return BitConverter.ToInt32(a32, 0);
    }

    public override Int16 ReadInt16()
    {
        a16 = base.ReadBytes(2);
        Array.Reverse(a16);
        return BitConverter.ToInt16(a16, 0);
    }

    [ . . . ] // All other types are converted accordingly.

}

This works fine till I assign the converted values like this:

ReverseBinaryReader binReader = new ReverseBinaryReader(new MemoryStream(content));

this.Size = binReader.ReadInt16();  // public short Size

For example, if I want to save the bytes: 0x00, 0x02 as big endian, I would expect this in the memory: 0x0200 however the short value of Size would become 0x0002. Why is that?

Any ideas? Thanks,
Peer

// Edit 2:

To clear the issue up a little I’ll try to show an example:

public class Message {
    public short Size;
    public byte[] Content;

    public Message(byte[] rawData)
    {
        ReverseBinaryReader binReader = new ReverseBinaryReader(new MemoryStream(rawData));

        this.Size = binReader.ReadInt16();  // public short Size
        this.Content = binReader.ReadBytes(2); // These are not converted an work just fine.
    }
}

public class prog {
     public static int main()
     {
          TCPClient aClient = new TCPClient("127.0.0.1",999); // Async socket
          aClient.Send(new Message(new byte[] { 0x00, 0x02 } );
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:41:20+00:00Added an answer on June 12, 2026 at 3:41 pm

    Edit:

    Moved this to the top, since it is the actual answer.

    I had to stare at it for far to long, but here is the source of your error.

    • Your initial array is { 0x00, 0x02 } In LE that is 0x0200, BE is 0x0002
    • You send it across the network, and read the first two bytes still { 0x00, 0x02 }
    • You reverse it giving you {0x02, 0x00} LE is 0x0002, BE is 0x0200
    • Your architecture is Little Endian so you get the “correct” result of 0x0002.

    Ergo the error is in your test array. You can verify that your Architecure is little endian by checking the IsLittleEndian property on BitConverter.

    =====================================================
    

    The following code is to help clarify, more than it is to answer your direct question. And to show that BitConverter is predictable on all architectures.

    using System;
    
    namespace BitConverterTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                UInt32 u32 = 0x01020304;
    
                byte[] u32ArrayLittle = {0x04, 0x03, 0x02, 0x01};
                byte[] u32ArrayBig = {0x01, 0x02, 0x03, 0x04};
    
                if (BitConverter.IsLittleEndian)
                {
                    if (BitConverter.ToUInt32(u32ArrayLittle, 0) != u32)
                    {
                        throw new Exception("Failed to convert the Little endian bytes");
                    }
                    Array.Reverse(u32ArrayBig); // convert the bytes to LittleEndian since that is our architecture.
                    if (BitConverter.ToUInt32(u32ArrayBig, 0) != u32)
                    {
                        throw new Exception("Failed to convert the Big endian bytes");
                    }
                } else
                {
                    Array.Reverse(u32ArrayLittle); // we are on a big endian platform
                    if (BitConverter.ToUInt32(u32ArrayLittle, 0) != u32)
                    {
                        throw new Exception("Failed to convert the Little endian bytes");
                    }
                    if (BitConverter.ToUInt32(u32ArrayBig, 0) != u32)
                    {
                        throw new Exception("Failed to convert the Big endian bytes");
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my website, I'm considering using Less . Can I have some problem of
I enjoy developing algorithms using the STL, however, I have this recurring problem where
I have been considering the problems arising with user authentication, using sessions/cookies and the
I have problem with my query on C, I’m using the oci8 driver. This
I have problem while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem that can essentially be viewed as a graph. I'm considering
Possible Duplicate: Using IoC for Unit Testing I think I do have a Problem
Guys I have problem to make a random matrix by considering some constraint and
I have a problem regarding to the software deployment. We're using JBoss 4.2.3. Please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.