RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
byte常用扩展有哪些

这篇文章给大家分享的是有关byte常用扩展有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

公司主营业务:成都做网站、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出剑川免费做网站回馈大家。

byte常用扩展应用一:转换为十六进制字符串

 public static string ToHex(this byte b)   {       return b.ToString("X2");   }    public static string ToHex(this IEnumerable< byte> bytes)   {       var sb = new StringBuilder();       foreach (byte b in bytes)          sb.Append(b.ToString("X2"));      return sb.ToString();  }

第二个扩展返回的十六进制字符串是连着的,一些情况下为了阅读方便会用一个空格分开,处理比较简单,不再给出示例。

byte常用扩展应用二:转换为Base64字符串

public static string ToBase64String(byte[] bytes)  {      return Convert.ToBase64String(bytes);  }

byte常用扩展应用三:转换为基础数据类型

public static int ToInt(this byte[] value, int startIndex)  {      return BitConverter.ToInt32(value, startIndex);  }  public static long ToInt64(this byte[] value, int startIndex)  {      return BitConverter.ToInt64(value, startIndex);  }

BitConverter类还有很多方法(ToSingle、ToDouble、ToChar...),可以如上进行扩展。

byte常用扩展应用四:转换为指定编码的字符串

public static string Decode(this byte[] data, Encoding encoding)  {      return encoding.GetString(data);  }

byte常用扩展应用五:Hash

 //使用指定算法Hash   public static byte[] Hash(this byte[] data, string hashName)   {       HashAlgorithm algorithm;       if (string.IsNullOrEmpty(hashName)) algorithm = HashAlgorithm.Create();       else algorithm = HashAlgorithm.Create(hashName);       return algorithm.ComputeHash(data);   }   //使用默认算法Hash  public static byte[] Hash(this byte[] data)  {      return Hash(data, null);  }

byte常用扩展应用六:位运算

 //index从0开始   //获取取第index是否为1   public static bool GetBit(this byte b, int index)   {       return (b & (1 < <  index)) > 0;   }   //将第index位设为1   public static byte SetBit(this byte b, int index)   {      b |= (byte)(1 < <  index);      return b;  }  //将第index位设为0  public static byte ClearBit(this byte b, int index)  {      b &= (byte)((1 < <  8) - 1 - (1 < <  index));      return b;  }  //将第index位取反  public static byte ReverseBit(this byte b, int index)  {      b ^= (byte)(1 < <  index);      return b;  }

byte常用扩展应用七:保存为文件

public static void Save(this byte[] data, string path)  {      File.WriteAllBytes(path, data);  }

byte常用扩展应用八:转换为内存流

public static MemoryStream ToMemoryStream(this byte[] data)  {      return new MemoryStream(data);  }

感谢各位的阅读!关于“byte常用扩展有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网页名称:byte常用扩展有哪些
链接分享:http://scyingshan.cn/article/pjicis.html