Encode byte array to Stream - CSharp System

CSharp examples for System:Byte Array

Description

Encode byte array to Stream

Demo Code

// The MIT License (MIT)
using System.IO;/* w w w  .  j  av  a  2  s  . c  o m*/
using System;

public class Main{
        public static unsafe void Encode(float value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(float*)val = value;
                    stream.Write(buffer, 0, sizeof(float));
                }
            }
        public static unsafe void Encode(double value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(double*)val = value;
                    stream.Write(buffer, 0, sizeof(double));
                }
            }
        public static unsafe void Encode(ulong value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(ulong*)val = value;
                    stream.Write(buffer, 0, sizeof(ulong));
                }
            }
        public static unsafe void Encode(long value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(long*)val = value;
                    stream.Write(buffer, 0, sizeof(long));
                }
            }
        public static unsafe void Encode(uint value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(uint*)val = value;
                    stream.Write(buffer, 0, sizeof(uint));
                }
            }
        public static unsafe void Encode(int value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(int*)val = value;
                    stream.Write(buffer, 0, sizeof(int));
                }
            }
        public static unsafe void Encode(ushort value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(ushort*)val = value;
                    stream.Write(buffer, 0, sizeof(ushort));
                }
            }
        public static unsafe void Encode(short value, byte[] buffer, Stream stream)
            {
                fixed (byte* val = buffer)
                {
                    *(short*)val = value;
                    stream.Write(buffer, 0, sizeof(short));
                }
            }
}

Related Tutorials