Get big-endian from a string?

1

I have a string "730C" and I need to get a INT big-endian

string packetString = "1F:73:0C:01:00:0E:01:01:29:35:1D:00:02:00:01:00:00:00:E7:03:0B:00:65:73:74:65:73:69:74:72:61:62:61:02:01:00:00:00:00";

        string ChrIndx = packetString.Substring(3, 5);
        string chrinx = ChrIndx.Replace(":", "");

        // convertimos el texto a numero
        int num = Int32.Parse(chrinx, System.Globalization.NumberStyles.HexNumber);

        //Console.WriteLine(num);

        //// Int a Byte
        byte[] byteNumero = BitConverter.GetBytes(num);

        ////Pasamos de little a Big endian
        if (!BitConverter.IsLittleEndian)
            Array.Reverse(byteNumero); //reverse 
        int result = BitConverter.ToInt32(byteNumero, 0);
        Console.WriteLine(result);
    
asked by Alejandro Maisonnat 13.03.2016 в 19:17
source

2 answers

1

I found a way to solve my problem! Next the code.

string packetString = "1F:73:0C:01:00:0E:01:01:29:35:1D:00:02:00:01:00:00:00:E7:03:0B:00:65:73:74:65:73:69:74:72:61:62:61:02:01:00:00:00:00";

        string ChrIndx = packetString.Substring(3, 5);
        string chrinx = ChrIndx.Replace(":", "");

        // convertimos el texto a numero
        int num = Int32.Parse(chrinx, System.Globalization.NumberStyles.HexNumber);

            // Int a Byte
            byte[] byteNumero = BitConverter.GetBytes(num);

            //Pasamos de little a Big endian
            if(BitConverter.IsLittleEndian)
            Array.Reverse(byteNumero); //reverse 
            int result = BitConverter.ToUInt16(byteNumero, 2);
            Console.WriteLine(result);
    
answered by 14.03.2016 / 00:52
source
-2

You could analyze the class

BitConverter (Class)

using something like being

    int number = Convert.ToInt32("730C", 16);
    byte[] bytes = BitConverter.GetBytes(number);

    int result = BitConverter.ToInt32(bytes, 0);

    Console.WriteLine(result);

convert string of hex to string of little endian in c #

    
answered by 13.03.2016 в 21:04