C# 中 byte[] 操作的类 方便 传送数据时添加多种数据类型的内容
来源:互联网 发布:美工设计工作流程 编辑:程序博客网 时间:2024/05/16 03:42
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ZicoxPrintTestTool{ class Bytes { public byte[] m_ByteArry; public Int32 m_Pos; private Int32 m_MaxSize; public Bytes(Int32 size) { m_ByteArry = new byte[size]; m_Pos = 0; m_MaxSize = size; } public bool AddCh(byte ch) { if (m_Pos>(m_MaxSize-1)) return false; m_ByteArry[m_Pos++] = ch; return true; } public bool AddBytes(byte byte1,byte byte2) { if ((m_Pos + 2) > m_MaxSize) return false; AddCh(byte1); AddCh(byte2); return true; } public bool AddBytes(byte byte1, byte byte2, byte byte3) { if ((m_Pos + 3) > m_MaxSize) return false; AddCh(byte1); AddCh(byte2); AddCh(byte3); return true; } public bool AddBytes(byte byte1, byte byte2, byte byte3, byte byte4) { if ((m_Pos + 4) > m_MaxSize) return false; AddCh(byte1); AddCh(byte2); AddCh(byte3); AddCh(byte4); return true; } public bool AddBytes(byte byte1, byte byte2, byte byte3, byte byte4, byte byte5) { if ((m_Pos + 5) > m_MaxSize) return false; AddCh(byte1); AddCh(byte2); AddCh(byte3); AddCh(byte4); AddCh(byte5); return true; } public bool AddBytes(byte[] bytes1,Int32 size) { if((size+m_Pos)>m_MaxSize) return false; for (int i = 0; i < size; i++) { AddCh(bytes1[i]); } return true; } public bool AddString(string str) { int len = System.Text.Encoding.Default.GetBytes(str).Length; if((len+m_Pos)>m_MaxSize) return false; byte[] tembuf = new byte[256]; tembuf = System.Text.Encoding.Default.GetBytes(str); for (int i = 0; i < len; i++) { AddCh(tembuf[i]); } return true; } public void Reset() { for (int i = 0; i < m_MaxSize; i++) m_ByteArry[i] = 0; m_Pos = 0; } }}