Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte array from small to big endian or vice versa

How would I convert a byte array, Byte[] from small to big endian.

I'm thinking of porting this program to Mono and was wondering the best approach. Any help would be appreciated.

EDIT: I'm reading from a file both on widows and mono.

Thanks. Bob.

like image 651
scope_creep Avatar asked Jan 10 '11 16:01

scope_creep


3 Answers

You cannot "convert a byte[] to big endian" unless you know what is in the byte[]. For example, 2-byte integers will need to have their two bytes swapped, while 4-byte integers will need to have their 4 bytes reversed. If the array only contains one integer then reversing the elements will work. If not you will need to process each distinct entity contained in the array separately.

Mono.DataConvert is a library that can help here, if you know what segments of the array need to be treated as what kind of data type. I highly recommend checking out this library; I'm using it in several projects and it's pretty nifty. It's even MIT licensed and is contained within one source file, so you can just compile it directly into whatever assembly you are building.

like image 85
cdhowie Avatar answered Sep 23 '22 14:09

cdhowie


Hmm I think there is a problem here. A byte array is seldom inherently big endian or little endian. A byte is almost always endian independent. What you probably want is to correct the endianness of the integers, chars etc. contained in the byte array.

In order to do so you must first identify the bytes which constitute the said integer, char etc. and then flip those bytes. Simply flipping/reversing the whole array may not work (unless the whole array represents one single integer, char etc.)

Use the following overload of the Array.Reverse() method to do what you want...

public static void Reverse(
    Array array,
    int index,
    int length
)
like image 21
Autodidact Avatar answered Sep 22 '22 14:09

Autodidact


In my case, I needed to convert from a given key (that was already in little endian format) to big endian string. It was easier than I first imagined, and did not require shifting bytes. Here's a simple console application I used to test it:

    using System;
    using System.Text;

    public class Program
    {
        public static void Main()
        {   
            string key = "B13E745599654841172F741A662880D4";
            var guid = new Guid(key);
            string hex = HexStringFromBytes(guid.ToByteArray());
            Console.WriteLine("Original key: " + guid);
            Console.WriteLine();                
            Console.WriteLine("Big-endian version of the key: " + hex);
            Console.ReadLine();
        }

        public static string HexStringFromBytes(byte[] bytes)
        {
            var sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                var hex = b.ToString("x2");
                sb.Append(hex);
            }
            return sb.ToString();
        }
    }

The given example prints this in the console:

Original key: B13E7455-9965-4841-172F-741A662880D4

Big-endian version of the key: 55743eb165994148172f741a662880d4

You can find a working fiddle here: Little Endian to Big Endian

like image 21
Damian Perez Avatar answered Sep 21 '22 14:09

Damian Perez