Converts a value to little-endian - CSharp System

CSharp examples for System:Char Unicode

Description

Converts a value to little-endian

Demo Code

//     Copyright (c) Outbreak Labs. All rights reserved.
using System.Net;

public class Main{
        /// <summary>
        ///     Converts a value to little-endian
        /// </summary>
        /// <param name="host">Value in big-endian</param>
        /// <returns>Value in little-endian</returns>
        public static ushort NetworkToHostOrder(ushort host)
        {//from  ww w .j a v a 2s. c om
            return (ushort)(((host & 0xFF) << 8) | ((host >> 8) & 0xFF));
        }
        /// <summary>
        ///     Converts a value to little-endian
        /// </summary>
        /// <param name="source">Value in big-endian</param>
        /// <returns>Value in little-endian</returns>
        public static uint NetworkToHostOrder(uint source)
        {
            return ((source & 0x000000FF) << 24) | ((source & 0x0000FF00) << 8) | ((source & 0x00FF0000) >> 8)
                   | ((source & 0xFF000000) >> 24);
        }
        /// <summary>
        ///     Converts a value to little-endian
        /// </summary>
        /// <param name="host">Value in big-endian</param>
        /// <returns>Value in little-endian</returns>
        public static short NetworkToHostOrder(short host)
        {
            return (short)(((host & 0xFF) << 8) | ((host >> 8) & 0xFF));
        }
}

Related Tutorials