C# byte数组常用扩展浅析

开发 后端
C# byte数组常用扩展有哪些呢?C# byte数组常用扩展具体的实现实例是什么呢?那么这里我们就将介绍C# byte数组常用扩展以及实现实例。
C# byte数组常用扩展是我们编程中经常会碰到的一些实用性很强的操作,那么C# byte数组常用扩展都有哪些呢?下面将列出并用实例演示常用八种情况。

C# 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();  
  12.  } 

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

C# byte数组常用扩展应用二:转换为Base64字符串

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

C# 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);  
  8.  } 

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

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

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

C# 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);  

C# 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;  
  24.  } 

C# byte数组常用扩展应用七:保存为文件

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

C# byte数组常用扩展应用八:转换为内存流

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

C# byte数组常用扩展的八种情况就向你介绍到这里,希望对你了解和学习C# byte数组常用扩展有所帮助。

【编辑推荐】

  1. C#静态方法与非静态方法的比较
  2. C#静态方法应用实例详解
  3. C#反射概念以及实例详解
  4. C#反射命名空间浅析
  5. C#静态类和静态类成员详解
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-10 17:36:17

C#扩展方法

2009-08-26 13:07:07

C#交错数组

2009-08-27 18:04:01

c#扩展方法string

2009-08-31 14:56:32

C# Byte数组转换

2009-08-31 14:46:15

C# string b

2009-08-17 17:56:32

C# 枚举

2009-08-06 10:14:15

C#引用类型数组

2009-08-07 13:39:13

C#定义整型数组

2009-09-02 10:58:02

C#动态数组

2009-09-03 17:18:40

C#扩展性对象模型

2009-09-18 10:58:31

C#数组操作

2009-08-26 10:09:52

byte常用扩展

2009-09-23 09:36:34

C#数组

2009-08-28 14:54:20

C# byte数组

2009-09-17 18:07:22

C#动态数组

2009-09-07 10:34:47

2009-08-13 13:03:52

C#结构体数组

2009-09-17 15:39:56

C#数组初始化

2009-08-07 17:25:37

C# SortedLi

2009-08-17 18:34:50

C# ChangeCo
点赞
收藏

51CTO技术栈公众号