对象字节流转换

Posted yyjackma

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象字节流转换相关的知识,希望对你有一定的参考价值。

数据表示时间   长度(字节)   数据类型   描述及要求
平台登入时间     6          BYTE[6]      (每个字节分别代表:年、月、日、时、分、秒)
登入流水号           2          WORD     每登入一次,登入流水号自动加1
平台用户名      12            STRING     平台登入用户名
平台密码     20                     STRING   平台登入密码
加密规则             1          BYTE          0x01:数据不加密;0x02:RSA加密;0x03:AES128加密...

 

//平台登入类
public class EvPlatformLoginMsg
{
    public DateTime LoginTime{get;set;}

    public ushort SerialNumver{get;set;}

    public string Account{get;set}

    public string Password{get;set;}

    public EvEncryptionMode EncryptMode{get;set;}

    public EvPlatformLoginMsg(byte[] bytes, int offset)
    {
        EvDateTime evDateTime = new EvDateTime(bytes, offset);
        LoginTime = evDateTime.DateTime;
        offset += evDateTime.BytesSize;

        NetBytesUtil.EnsureNetOrder(bytes, index, 2);
        SerialNumver = BitConverter.ToUint16(bytes, index);
        offset += 2;

        Account = Encoding.ASCII.GetString(bytes, index, 12).TrimEnd(\0);
        offset += 12;

        Password = Encoding.ASCII.GetString(bytes, index, 20);
        offset += 20;

        EncryptMode = (EvEncryptionMode)bytes[index];
    }

    public byte[] ToBytes()
    {
        byte[] bytes = new byte[41];
        int offset = 0;

        offset += new EvDateTime(LoginTime).WriteToBytes(bytes, offset);

        BitExtend.WriteUint16ToBytes(bytes, offset, SerialNumver);
        NetBytesUtil.EnsureNetOrder(bytes, index, 2);
        index += 2;

        byte[] accountBytes = Encoding.ASCII.GetBytes(Account);
        Buffer.BlockCopy(accountBytes, 0, bytes, index, accountBytes.Length > 12 ? 12 : accountBytes.Length);
        index += 12;

        byte[] passwordBytes = Encoding.ASCII.GetBytes(Password);
        Buffer.BlockCopy(passwordBytes, 0, bytes, index, passwordBytes.Length > 20 ? 20 : passwordBytes.Length);
        index += 20;

        bytes[index] = (byte)EncryptMode;

        return bytes;
    }
}

//时间类型
public class EvDateTime
{
    private const int ConstBytesSize = 6;    //6个字节表示时间

    public DateTime DateTime 
    {
        get;
        set;
    }

    public EvDateTime(DateTime dateTime)
    {
        DateTime = dateTime;
    }

    public int BytesSize
    {
        get{return ConstBytesSize;}
        set{;}
    }

    public EvDateTime(byte[] bytes, int offset)
    {
        int year = byte[offset] + 2000;
        int month = byte[offset + 1];
        int day = byte[offset + 2];
        int hour = byte[offset + 3];
        int miniute = byte[offset + 4];
        int second = byte[offset + 5];
        DateTime = new DateTime(year, month, day, hour, miniute, second);
    }

    public int WriteToBytes(byte[] bytes, int offset)
    {
        bytes[offset] = (byte)(DateTime.Year - 2000);
        bytes[offset + 1] = (byte)DateTime.Month;
        bytes[offset + 2] = (byte)DateTime.Day;
        bytes[offset + 3] = (byte)DateTime.Hour;
        bytes[offset + 4] = (byte)DateTime.Minute;
        bytes[offset + 5] = (byte)DateTime.Second;
        return ConstBytesSize;
    }
}

//BitExtend类
public class BitExtend
{
    public static unsafe void WriteCharToBytes(byte[] bytes, int start, char value)
    {
        WriteInt16ToBytes(bytes, start, (short)val);
    }

    public static unsafe void WriteSingleToBytes(byte[] bytes, int start, float value)
    {
        WriteInt32ToBytes(bytes, start, *((int*)&value));
    }

    public static unsafe void WriteDoubleToBytes(byte[] bytes, int start, double value)
    {
        WriteInt64ToBytes(bytes, start, *((long*)&value));
    }

    public static unsafe void WriteBooleanToBytes(byte[] bytes, int start, boolean value){
        bytes[start] = value ? (byte)1 : (byte)0;
    }

    public static unsafe void WriteUint16ToBytes(byte[] bytes, int start, ushort value)
    {
        WriteInt16ToBytes(bytes, start, (short)value);
    }

    public static unsafe void WriteUint32ToBytes(byte[] bytes, int start, uint value)
    {
        WriteInt32ToBytes(bytes, start, (short)value);
    }

    public static unsafe void WriteUint64ToBytes(byte[] bytes, int start, ulong value)
    {
        WriteInt64ToBytes(bytes, start, (long)value);
    }

    public static unsafe void WriteInt32ToBytes(byte[] bytes, int start, int value)
    {
        fixed(byte* numRef = bytes)
        {
            *((int*)(numRef = start)) = value;
        }
    }

    public static unsafe void WriteInt16ToBytes(byte[] bytes, int start, short value)
    {
        fixed (byte* numRef = bytes)
        {
            *((short*)(numRef + start)) = value;
        }
    }

    public static unsafe void WriteInt32ToBytes(byte[] bytes, int start, int value)
    {
        fixed(byte* numRef = bytes)
        {
            *((int*)(numRef + start)) = value;
        }
    }

    public static unsafe void WriteInt64ToBytes(byte[] bytes, int start, long value)
    {
        fixed(long* numRef = bytes) 
        {
            *((long*)(numRef + start)) = value;
        }
    }
}

//网络序和本机序
public static class NetBytesUtil
{
    public static int[] Byte2Bits(byte b)
    {
        int[] bitArray = new int[8];
        bitArray[0] = b & 1;
        bitArray[1] = b & 2;
        bitArray[2] = b & 4;
        bitArray[3] = b & 8;
        bitArray[4] = b & 16;
        bitArray[5] = b & 32;
        bitArray[6] = b & 64;
        bitArray[7] = b & 128;

        return bitArray;
    }

    public static bool TestBit(long src, byte bitIndex)
    {
        if (src == 0)
        {
            return false;
        }
        else
        {
            return ((ulong)src & (ulong)Math.Pow(2, bitIndex)) == (ulong)Math.Pow(2, bitIndex);
        }
    }

    public static long SetBit(long src, byte bitIndex)
    {
        return src | (long)Math.Pow(2, bitIndex);
    }
    public static void EnsureNetOrder(byte[] bytes, int start, int size)
    {
        if (BitConverter.IsLittleEndian)
        {
            if (bytes == null)
            {
                throw new Exception("bytes is null");
            }
            if (start < 0)
            {
                throw new Exception("start less than 0");
            }
            if (size < 0)
            {
                throw new Exception("size less than 0");
            }
            if (bytes.Length < start + size)
            {
                throw new Exception("bytes length less then that start plus size");
            }
            if (size > 1)
            {
                byte tmp;
                for (int i = 0; i < size / 2; i++)
                {
                    tmp = bytes[start + i];
                    bytes[start + i] = bytes[start + size - 1 - i];
                    bytes[start + size - 1 - i] = tmp;
                }
            }
        }
    }

    public static void EnsureHostOrder(byte[] bytes, int start, int size)
    {
        if (!BitConverter.IsLittleEndian)
        {
            if (bytes == null)
            {
                throw new Exception("bytes is null");
            }
            if (start < 0)
            {
                throw new Exception("start less than 0");
            }
            if (size < 0)
            {
                throw new Exception("size less than 0");
            }
            if (bytes.Length < start + size)
            {
                throw new Exception("bytes length less then that start plus size");
            }
            if (size > 1)
            {
                byte tmp;
                for (int i = 0; i < size / 2; i++)
                {
                    tmp = bytes[start + i];
                    bytes[start + i] = bytes[start + size - 1 - i];
                    bytes[start + size - 1 - i] = tmp;
                }
            }
        }
    }

    public static void WriteIp(byte[] bytes, int start, string ip)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        if (bytes.Length < start + 4)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        if (string.IsNullOrEmpty(ip))
        {
            throw new Exception("ip is null or empty");
        }

        string[] array = ip.Split(new char[] { . }, StringSplitOptions.RemoveEmptyEntries);
        if (array.Length != 4)
        {
            throw new Exception("ip format is error");
        }

        bytes[start] = byte.Parse(array[0]);
        bytes[start + 1] = byte.Parse(array[1]);
        bytes[start + 2] = byte.Parse(array[2]);
        bytes[start + 3] = byte.Parse(array[3]);
    }

    public static string ReadIp(byte[] bytes, int start)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }
        if (bytes.Length < start + 4)
        {
            throw new Exception("bytes length less then that start plus size");
        }
        return string.Format("{0}.{1}.{2}.{3}", bytes[start], bytes[start + 1], bytes[start + 2], bytes[start + 3]);
    }

    [Obsolete]
    public static void WriteInt16ToBytes(byte[] bytes, int start, short val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 2;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
    }

    [Obsolete]
    public static void WriteInt32ToBytes(byte[] bytes, int start, int val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 4;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
        bytes[start + 2] = (byte)(val >> 16 & 0xff);
        bytes[start + 3] = (byte)(val >> 24 & 0xff);
    }

    [Obsolete]
    public static void WriteInt64ToBytes(byte[] bytes, int start, long val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 8;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
        bytes[start + 2] = (byte)(val >> 16 & 0xff);
        bytes[start + 3] = (byte)(val >> 24 & 0xff);
        bytes[start + 4] = (byte)(val >> 32 & 0xff);
        bytes[start + 5] = (byte)(val >> 40 & 0xff);
        bytes[start + 6] = (byte)(val >> 48 & 0xff);
        bytes[start + 7] = (byte)(val >> 56 & 0xff);
    }

    [Obsolete]
    public static void WriteInt64To5Bytes(byte[] bytes, int start, long val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 5;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
        bytes[start + 2] = (byte)(val >> 16 & 0xff);
        bytes[start + 3] = (byte)(val >> 24 & 0xff);
        bytes[start + 4] = (byte)(val >> 32 & 0xff);
        //bytes[start + 5] = (byte)(val >> 40 & 0xff);
        //bytes[start + 6] = (byte)(val >> 48 & 0xff);
        //bytes[start + 7] = (byte)(val >> 56 & 0xff);
    }

    [Obsolete]
    public static void WriteUInt16ToBytes(byte[] bytes, int start, ushort val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 2;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        //bytes[start] = (byte)(val & 0x11);
        //bytes[start + 1] = (byte)((val & 0x1100) >> 8);

        bytes[start] = (byte)(0xff & val);
        bytes[start + 1] = (byte)((0xff00 & val) >> 8);
    }

    [Obsolete]
    public static void WriteUInt32ToBytes(byte[] bytes, int start, uint val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 4;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
        bytes[start + 2] = (byte)(val >> 16 & 0xff);
        bytes[start + 3] = (byte)(val >> 24 & 0xff);

    }

    [Obsolete]
    public static void WriteUInt64ToBytes(byte[] bytes, int start, ulong val)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = 8;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        bytes[start] = (byte)(val & 0xff);
        bytes[start + 1] = (byte)(val >> 8 & 0xff);
        bytes[start + 2] = (byte)(val >> 16 & 0xff);
        bytes[start + 3] = (byte)(val >> 24 & 0xff);
        bytes[start + 4] = (byte)(val >> 32 & 0xff);
        bytes[start + 5] = (byte)(val >> 40 & 0xff);
        bytes[start + 6] = (byte)(val >> 48 & 0xff);
        bytes[start + 7] = (byte)(val >> 56 & 0xff);
    }

    public static int WriteAsc2Bytes(byte[] bytes, int start, string str)
    {
        if (bytes == null)
        {
            throw new Exception("bytes is null");
        }
        if (start < 0)
        {
            throw new Exception("start less than 0");
        }

        int size = str.Length;

        if (bytes.Length < start + size)
        {
            throw new Exception("bytes length less then that start plus size");
        }

        for (int i = 0; i < str.Length; i++)
        {
            try
            {
                bytes[start + i] = Convert.ToByte(str[i]);
            }
            catch (Exception ex)
            {
                ExceptionUtil.WriteLog(ex);
                DebugUtil.WriteLog("错误字符" + str[i]);
            }
        }

        return start + size;
    }
}

 

以上是关于对象字节流转换的主要内容,如果未能解决你的问题,请参考以下文章

第十周学习总结

关于序列化:把某个对象序列化成字节流

Java学习之IO流(转换流)

C#文件转换为字节流及字节流转换为文件

将无符号字节流转换为有符号字节流 Golang

GBK编码字节流与UTF-8编码字节流的转换