巧思妙解byte常用扩展

开发 后端
本文介绍了byte常用扩展的8个应用,例如转换为十六进制字符串,转换为Base64字符串等。

本文介绍了byte常用扩展的8个应用,比较简单,没有太多技术含量,不用太多解释,主要提供大家一个思路,如需要使用,请自行完善。

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

  1.  public static string ToHex(this byte b)  
  2.  {  
  3.      return b.ToString("X2");  
  4.  }  
  5.  
  6.  public static string ToHex(this IEnumerable< byte> bytes)  
  7.  {  
  8.      var sb = new StringBuilder();  
  9.      foreach (byte b in bytes)  
  10.         sb.Append(b.ToString("X2"));  
  11.     return sb.ToString();  

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

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

  1. public static string ToBase64String(byte[] bytes)  
  2. {  
  3.     return Convert.ToBase64String(bytes);  

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

  1. public static int ToInt(this byte[] value, int startIndex)  
  2. {  
  3.     return BitConverter.ToInt32(value, startIndex);  
  4. }  
  5. public static long ToInt64(this byte[] value, int startIndex)  
  6. {  
  7.     return BitConverter.ToInt64(value, startIndex);  

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

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

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

byte常用扩展应用五:Hash

  1.  //使用指定算法Hash  
  2.  public static byte[] Hash(this byte[] data, string hashName)  
  3.  {  
  4.      HashAlgorithm algorithm;  
  5.      if (string.IsNullOrEmpty(hashName)) algorithm = HashAlgorithm.Create();  
  6.      else algorithm = HashAlgorithm.Create(hashName);  
  7.      return algorithm.ComputeHash(data);  
  8.  }  
  9.  //使用默认算法Hash  
  10. public static byte[] Hash(this byte[] data)  
  11. {  
  12.     return Hash(data, null);  

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

  1.  //index从0开始  
  2.  //获取取第index是否为1  
  3.  public static bool GetBit(this byte b, int index)  
  4.  {  
  5.      return (b & (1 < <  index)) > 0;  
  6.  }  
  7.  //将第index位设为1  
  8.  public static byte SetBit(this byte b, int index)  
  9.  {  
  10.     b |= (byte)(1 < <  index);  
  11.     return b;  
  12. }  
  13. //将第index位设为0  
  14. public static byte ClearBit(this byte b, int index)  
  15. {  
  16.     b &= (byte)((1 < <  8) - 1 - (1 < <  index));  
  17.     return b;  
  18. }  
  19. //将第index位取反  
  20. public static byte ReverseBit(this byte b, int index)  
  21. {  
  22.     b ^= (byte)(1 < <  index);  
  23.     return b;  

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

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

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

  1. public static MemoryStream ToMemoryStream(this byte[] data)  
  2. {  
  3.     return new MemoryStream(data);  

【编辑推荐】

  1. 总结C#语言命名规范
  2. C#反射相关知识学习
  3. 大话F#和C#:是否会重蹈C#失败的覆辙?
  4. 总结和学习C#接口
  5. 学习C#程序有感
责任编辑:book05 来源: cnblogs
相关推荐

2009-08-28 14:25:57

C# byte数组

2009-04-27 21:28:56

2009-11-23 08:53:33

2010-10-08 16:29:07

2009-08-27 18:04:01

c#扩展方法string

2021-02-02 21:42:30

VS Code编辑器开发

2020-08-03 11:17:15

APP设计产品

2018-01-02 14:57:59

谷歌面试算法

2009-12-18 14:19:01

无线扩展通信技术特点

2009-11-26 14:40:51

无线路由器

2024-03-11 10:19:30

Plasmo浏览器Web

2009-11-10 15:18:03

思科路由器常用配置命令

2009-01-03 08:56:00

局域网服务优化

2015-11-02 09:16:08

2024-01-03 16:37:26

Jupyter工具开源

2023-06-12 07:00:40

Rust进度任务

2011-07-13 09:59:55

2022-05-17 07:26:33

动画CSS前端

2021-07-26 10:15:10

哈希字母异位词
点赞
收藏

51CTO技术栈公众号