Convert Byte[] to short[] in memory order. Like Marshal buffer copy. - CSharp System

CSharp examples for System:Byte

Description

Convert Byte[] to short[] in memory order. Like Marshal buffer copy.

Demo Code

//     Copyright (c) Microsoft Corporation.  All rights reserved.
using System.Globalization;
using System;/*from  w  w w  .ja  va  2 s . co m*/

public class Main{
        /// <summary>
        /// Convert Byte[] to short[] in memory order.
        /// Like Marshal buffer copy.
        /// </summary>
        /// <param name="from">Source array to convert.</param>
        /// <returns>Result short array.</returns>
        public static short[] BinaryConvertArray(byte[] from)
        {
            if (from == null)
            {
                throw new ArgumentNullException("from");
            }

            short[] to = new short[from.Length >> 1];
            Buffer.BlockCopy(from, 0, to, 0, from.Length);
            return to;
        }

        /// <summary>
        /// Convert short[] to Byte[] in memory order
        /// Like Marshal buffer copy.
        /// </summary>
        /// <param name="from">Source array to convert.</param>
        /// <returns>Result byte array.</returns>
        public static byte[] BinaryConvertArray(short[] from)
        {
            if (from == null)
            {
                throw new ArgumentNullException("from");
            }

            byte[] to = new byte[from.Length << 1];
            Buffer.BlockCopy(from, 0, to, 0, to.Length);
            return to;
        }
}

Related Tutorials