Converts from Little Endian to Big Endian - CSharp System

CSharp examples for System:String Convert

Description

Converts from Little Endian to Big Endian

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w w w  .j  av a 2 s .  co m

public class Main{
        /// <summary>
        /// Converts from Little Endian to Big Endian
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static string ToBigEndian(this string hex)
        {
            if (hex.Length % 2 == 1)
                return hex;

            List<string> pairs = new List<string>();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hex.Length - 1; i += 2)
                pairs.Add(hex.Substring(i, 2));

            pairs.Reverse();

            foreach (string s in pairs)
                sb.Append(s);

            return sb.ToString();
        }
}

Related Tutorials