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 159793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:55:17+00:00 2026-05-11T10:55:17+00:00

Assumption: Converting a byte[] from Little Endian to Big Endian means inverting the order

  • 0

Assumption:

Converting a byte[] from Little Endian to Big Endian means inverting the order of the bits in each byte of the byte[].

Assuming this is correct, I tried the following to understand this:

byte[] data = new byte[] { 1, 2, 3, 4, 5, 15, 24 }; byte[] inverted = ToBig(data);  var little = new BitArray(data); var big = new BitArray(inverted);  int i = 1;  foreach (bool b in little) {     Console.Write(b ? '1' : '0');     if (i == 8)     {         i = 0;         Console.Write(' ');     }     i++; }  Console.WriteLine();  i = 1;  foreach (bool b in big) {     Console.Write(b ? '1' : '0');     if (i == 8)     {         i = 0;         Console.Write(' ');     }     i++; }  Console.WriteLine();  Console.WriteLine(BitConverter.ToString(data)); Console.WriteLine(BitConverter.ToString(ToBig(data)));  foreach (byte b in data) {     Console.Write('{0} ', b); }  Console.WriteLine();  foreach (byte b in inverted) {     Console.Write('{0} ', b); } 

The convert method:

private static byte[] ToBig(byte[] data) {     byte[] inverted = new byte[data.Length];      for (int i = 0; i < data.Length; i++)     {         var bits = new BitArray(new byte[] { data[i] });         var invertedBits = new BitArray(bits.Count);          int x = 0;          for (int p = bits.Count - 1; p >= 0; p--)         {             invertedBits[x] = bits[p];             x++;         }          invertedBits.CopyTo(inverted, i);     }      return inverted; } 

The output of this little application is different from what I expected:

00000001 00000010 00000011 00000100 00000101 00001111 00011000  00000001 00000010 00000011 00000100 00000101 00001111 00011000   80-40-C0-20-A0-F0-18  01-02-03-04-05-0F-18  1 2 3 4 5 15 24   1 2 3 4 5 15 24  

For some reason the data remains the same, unless printed using BitConverter.

What am I not understanding?

Update

New code produces the following output:

10000000 01000000 11000000 00100000 10100000 11110000 00011000   00000001 00000010 00000011 00000100 00000101 00001111 00011000   01-02-03-04-05-0F-18  80-40-C0-20-A0-F0-18  1 2 3 4 5 15 24   128 64 192 32 160 240 24  

But as I have been told now, my method is incorrect anyway because I should invert the bytes and not the bits?

This hardware developer I’m working with told me to invert the bits because he cannot read the data.

Context where I’m using this

The application that will use this does not really work with numbers.

I’m supposed to save a stream of bits to file where

1 = white and 0 = black.

They represent pixels of a bitmap 256×64.

byte 0 to byte 31 represents the first row of pixels byte 32 to byte 63 the second row of pixels.

I have code that outputs these bits… but the developer is telling me they are in the wrong order… He says the bytes are fine but the bits are not.

So I’m left confused :p

  • 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. 2026-05-11T10:55:17+00:00Added an answer on May 11, 2026 at 10:55 am

    Your method may be correct at this point. There are different meanings of endianness, and it depends on the hardware.

    Typically, it’s used for converting between computing platforms. Most CPU vendors (now) use the same bit ordering, but different byte ordering, for different chipsets. This means, that, if you are passing a 2-byte int from one system to another, you leave the bits alone, but swap bytes 1 and 2, ie:

    int somenumber -> byte[2]: somenumber[high],somenumber[low] ->   byte[2]: somenumber[low],somenumber[high] -> int newNumber 

    However, this isn’t always true. Some hardware still uses inverted BIT ordering, so what you have may be correct. You’ll need to either trust your hardware dev. or look into it further.

    I recommend reading up on this on Wikipedia – always a great source of info:

    http://en.wikipedia.org/wiki/Endianness


    Your ToBig method has a bug.

    At the end:

     invertedBits.CopyTo(data, i); }  return data; 

    You need to change that to:

    byte[] newData = new byte[data.Length]; invertedBits.CopyTo(newData, i); } return newData; 

    You’re resetting your input data, so you’re receiving both arrays inverted. The problem is that arrays are reference types, so you can modify the original data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In order to retrieve the current modelview matrix you have… May 11, 2026 at 3:18 pm
  • added an answer If you have few enough records it's almost certain that… May 11, 2026 at 3:18 pm
  • added an answer These fields are FAR too small for full text search… May 11, 2026 at 3:18 pm

Related Questions

If I have two colors defined by their RGB values, can I average the
I know I'll get a thousand Depends on what you're trying to do answers,
i'm trying to sort this nested list by inner's list first element: ak =
Assumption: live/production web app suppresses errors being shown to end-users. Suppose your tech support

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.