1. 程式人生 > >C#常用資料型別轉換工具類

C#常用資料型別轉換工具類

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Text;
/// <summary>
/// 資料轉換.
/// </summary>
public class MemoryStreamTool:MemoryStream{



    public MemoryStreamTool(byte[] buffer) : base(buffer)
    {

    }

    public MemoryStreamTool() { }
    #region  int
    /// <summary>
    /// 從流中讀取一個int資料.
    /// </summary>
    /// <returns></returns>
    public int ReadInt()
    {
        byte[] arr = new byte[4];
        base.Read(arr,0,arr.Length);
        return BitConverter.ToInt32(arr,0);
    }
    /// <summary>
    /// 把一個int陣列寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteInt(int value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr,0,arr.Length);
    }
    #endregion
    #region  uint
    /// <summary>
    /// 從流中讀取一個uint資料.
    /// </summary>
    /// <returns></returns>
    public uint ReaduInt()
    {
        byte[] arr = new byte[4];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToUInt32(arr,0);
    }
    /// <summary>
    /// 把一個uint陣列寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteuInt(uint value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion

    #region short

    /// <summary>
    /// 從流中讀取一個short資料
    /// </summary>
    /// <returns></returns>
    public short ReadShort()
    {
        byte[] arr = new byte[2];
        base.Read(arr,0,arr.Length);
        return BitConverter.ToInt16(arr,0);
    }
    /// <summary>
    /// 把一個short資料寫入流中.
    /// </summary>
    /// <param name="value"></param>
    public void WriteShort(short value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr,0,arr.Length);
    }
    #endregion
    #region ushort

    /// <summary>
    /// 從流中讀取一個ushort資料
    /// </summary>
    /// <returns></returns>
    public ushort ReaduShort()
    {
        byte[] arr = new byte[2];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToUInt16(arr, 0);
    }
    /// <summary>
    /// 把一個short資料寫入流中.
    /// </summary>
    /// <param name="value"></param>
    public void WriteuShort(ushort value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion

    #region long
    /// <summary>
    /// 從流中讀取一個long資料.
    /// </summary>
    /// <returns></returns>
    public long ReadLong()
    {
        byte[] arr = new byte[8];
        base.Read(arr,0,arr.Length);
        return BitConverter.ToInt64(arr,0);
    }
    /// <summary>
    /// 把一個long資料寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteLong(long value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr,0,arr.Length);
    }
    #endregion
    #region ulong
    /// <summary>
    /// 從流中讀取一個ulong資料.
    /// </summary>
    /// <returns></returns>
    public ulong ReaduLong()
    {
        byte[] arr = new byte[8];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToUInt64(arr, 0);
    }
    /// <summary>
    /// 把一個ulong資料寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteuLong(ulong value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion

    #region double
    /// <summary>
    /// 從流中讀取一個double資料.
    /// </summary>
    /// <returns></returns>
    public double ReadDouble()
    {
        byte[] arr = new byte[8];
        base.Read(arr,0,arr.Length);
        return BitConverter.ToDouble(arr,0);
    }
    /// <summary>
    /// 把一個double資料寫入流中.
    /// </summary>
    /// <param name="value"></param>
    public void WriteDouble(double value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr,0,arr.Length);
    }
    #endregion

    #region  float
    /// <summary>
    /// 從流中讀取一個float資料.
    /// </summary>
    /// <returns></returns>
    public float ReadFloat()
    {
        byte[] arr = new byte[4];
        base.Read(arr, 0, arr.Length);
        return BitConverter.ToSingle(arr, 0);
    }
    /// <summary>
    /// 把一個float陣列寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteFloat(float value)
    {
        byte[] arr = BitConverter.GetBytes(value);
        base.Write(arr, 0, arr.Length);
    }
    #endregion

    #region bool
    /// <summary>
    /// 從流中讀取一個bool資料.
    /// </summary>
    /// <returns></returns>
    public bool ReadBool()
    {
        return base.ReadByte() == 1;
    }

    /// <summary>
    /// 把一個bool資料寫入流.
    /// </summary>
    /// <param name="value"></param>
    public void WriteBool(bool value)
    {
        base.WriteByte((byte)(value==true?1:0));
    }
    #endregion

    #region string
    /// <summary>
    /// 從流中讀取一個string陣列.
    /// </summary>
    /// <returns></returns>
    public string ReadUTF8String()
    {
        ushort len = this.ReaduShort();
        byte[] arr = new byte[len];
        base.Read(arr,0,len);
        return Encoding.UTF8.GetString(arr);
    }

    /// <summary>
    /// 把一個string資料寫入流.
    /// </summary>
    /// <param name="str"></param>
    public void WriteUTF8String(string str)
    {
        byte[] arr = Encoding.UTF8.GetBytes(str);
        if (arr.Length > 65535)
        {
            throw new InvalidCastException("字串超出範圍.");
        }
        WriteuShort((ushort)arr.Length);
        base.Write(arr,0,arr.Length);
    }
    #endregion
}

測試後得到以下結果:

                                  int在記憶體中佔用4個位元組
                                 double在記憶體中佔用8個位元組
                                 long在記憶體中佔用8個位元組
                                 float在記憶體中佔用4個位元組
                                 short在記憶體中佔用2個位元組
                                 bool在記憶體中佔用1個位元組