Converting an array of bytes from small to large numbers or vice versa

How to convert an array of bytes, Byte [] from small to large.

I am thinking of porting this program to Mono and wondered which approach is best suited. Any help would be greatly appreciated.

EDIT: I read from a file in both widows and mono.

Thank. Bean.

+3
source share
4 answers

" byte[] ", , byte[]. , 2- , 4- 4 . , . , , , .

Mono.DataConvert - , , , . ; , . MIT , , .

+4

, , . . . , , , , - , .., .

, , , char .., . / ( , char ..)

Array.Reverse(), , ...

public static void Reverse(
    Array array,
    int index,
    int length
)
+2

( ) . , , . , :

    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();
        }
    }

:

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

: 55743eb165994148172f741a662880d4

: Endian to Big Endian

+1

Do you want to change the objects in the array or the value of the bytes themselves? If only an array, then you can useArray.Reverse()

0
source

Source: https://habr.com/ru/post/1784571/


All Articles