Byte Endian : UTF8 UTF16 « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » File Stream » UTF8 UTF16Screenshots 
Byte Endian
      
/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Utility
{
    using System;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;

    internal static class Protocol
    {
        internal static byte[] GetBigEndianBytes(this ushort v)
        {
            return BitConverter.GetBytes(v.SwapEndian());
        }

        internal static byte[] GetBigEndianBytes(this uint v)
        {
            return BitConverter.GetBytes(v.SwapEndian());
        }

        internal static byte[] GetBigEndianBytes(this ulong v)
        {
            return BitConverter.GetBytes(v.SwapEndian());
        }

        internal static ushort GetLittleEndianUInt16(this byte[] data)
        {
            return data.GetLittleEndianUInt16(0);
        }

        internal static uint GetLittleEndianUInt32(this byte[] data)
        {
            return data.GetLittleEndianUInt32(0);
        }

        internal static ulong GetLittleEndianUInt64(this byte[] data)
        {
            return data.GetLittleEndianUInt64(0);
        }

        internal static ushort GetLittleEndianUInt16(this byte[] data, int index)
        {
            return BitConverter.ToUInt16(data, index).SwapEndian();
        }

        internal static uint GetLittleEndianUInt32(this byte[] data, int index)
        {
            return BitConverter.ToUInt32(data, index).SwapEndian();
        }

        internal static ulong GetLittleEndianUInt64(this byte[] data, int index)
        {
            return BitConverter.ToUInt64(data, index).SwapEndian();
        }

        internal static T BytesToStruct<T>(this byte[] rawDatawhere T : IToBytes
        {
            T result = default(T);
            RespectEndianness(typeof(T), rawData);
            GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            try
            {
                IntPtr rawDataPtr = handle.AddrOfPinnedObject();
                result = (T)Marshal.PtrToStructure(rawDataPtr, typeof(T));
            }
            finally
            {
                handle.Free();
            }
            return result;
        }

        internal static byte[] StructToBytes<T>(this T datawhere T : IToBytes
        {
            byte[] rawData = new byte[Marshal.SizeOf(data)];
            GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            try
            {
                IntPtr rawDataPtr = handle.AddrOfPinnedObject();
                Marshal.StructureToPtr(data, rawDataPtr, false);
            }
            finally
            {
                handle.Free();
            }
            RespectEndianness(typeof(T), rawData);
            return rawData;
        }

        private static void RespectEndianness(Type type, byte[] data)
        {
            var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Select(field => new
            {
                Field = field,
                Offset = Marshal.OffsetOf(type, field.Name).ToInt32()
            }).ToList();

            fields.ForEach(item => Array.Reverse(data, item.Offset, Marshal.SizeOf(item.Field.FieldType)));
        }

        private static ushort SwapEndian(this ushort v)
        {
            return (ushort)(((v & 0xff<< 8((v >> 80xff));
        }

        private static uint SwapEndian(this uint v)
        {
            return (uint)(((SwapEndian((ushort)v0xffff<< 0x10|
                           (SwapEndian((ushort)(v >> 0x10)) 0xffff));
        }

        internal static ulong SwapEndian(this ulong v)
        {
            return (ulong)(((SwapEndian((uint)v0xffffffffL<< 0x20|
                            (SwapEndian((uint)(v >> 0x20)) 0xffffffffL));
        }
    }
}

   
    
    
    
    
    
  
Related examples in the same category
1.Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded stringConvert UTF-8 and ASCII encoded bytes back to UTF-16 encoded string
2.UTF8 decoder
3.String to ASCII
4.Guess Text File Encoding
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.