发新帖

[C#/ASP.NET] C# 去除byte数组里的特定值(空值)

零下一度 2022-12-13 751
class Program
{
  static void Main(string[] args)
  {
    byte[] b = new byte[10];
    b[0] = 0x00;
    b[1] = 0x26;
    b[2] = 0x17;
    b[3] = 0x18;
    byte cut = 0x00;
    byte[] lastbyte = byteCut( b, cut); // 此时的lastbyte 就是你需要的了..
    Console.ReadLine();
  }
  /// <summary>
  /// 去掉byte[] 中特定的byte
  /// </summary>
  /// <param name="b"> 需要处理的byte[]</param>
  /// <param name="cut">byte[] 中需要除去的特定 byte (此处: byte cut = 0x00 ;) </param>
  /// <returns> 返回处理完毕的byte[] </returns>
  public static byte[] byteCut(byte[] b, byte cut)
  {
    List<byte> list = new List<byte>();
    list.AddRange(b);
    for (int i = list.Count - 1; i >= 0; i--)
    {
      if (list[i] == cut )
      list.RemoveAt(i);
    }
    byte[] lastbyte = new byte[list.Count];
   for (int i = 0; i < list.Count; i++)
   {
    lastbyte[i] = list[i];
   }
   return lastbyte;
 }
 
}
最新回复 (0)
返回
零下一度
主题数
928
帖子数
0
注册排名
1