自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# byte數(shù)組常用擴展淺析

開發(fā) 后端
C# byte數(shù)組常用擴展有哪些呢?C# byte數(shù)組常用擴展具體的實現(xiàn)實例是什么呢?那么這里我們就將介紹C# byte數(shù)組常用擴展以及實現(xiàn)實例。
C# byte數(shù)組常用擴展是我們編程中經(jīng)常會碰到的一些實用性很強的操作,那么C# byte數(shù)組常用擴展都有哪些呢?下面將列出并用實例演示常用八種情況。

C# byte數(shù)組常用擴展應用一:轉換為十六進制字符串

  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數(shù)組常用擴展應用二:轉換為Base64字符串

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

C# byte數(shù)組常用擴展應用三:轉換為基礎數(shù)據(jù)類型

  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數(shù)組常用擴展應用四:轉換為指定編碼的字符串

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

C# byte數(shù)組常用擴展應用五: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數(shù)組常用擴展應用六:位運算

  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數(shù)組常用擴展應用七:保存為文件

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

C# byte數(shù)組常用擴展應用八:轉換為內(nèi)存流

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

C# byte數(shù)組常用擴展的八種情況就向你介紹到這里,希望對你了解和學習C# byte數(shù)組常用擴展有所幫助。

【編輯推薦】

  1. C#靜態(tài)方法與非靜態(tài)方法的比較
  2. C#靜態(tài)方法應用實例詳解
  3. C#反射概念以及實例詳解
  4. C#反射命名空間淺析
  5. C#靜態(tài)類和靜態(tài)類成員詳解
責任編輯:仲衡 來源: 博客園
相關推薦

2009-08-10 17:36:17

C#擴展方法

2009-08-26 13:07:07

C#交錯數(shù)組

2009-08-27 18:04:01

c#擴展方法string

2009-08-17 17:56:32

C# 枚舉

2009-08-06 10:14:15

C#引用類型數(shù)組

2009-08-07 13:39:13

C#定義整型數(shù)組

2009-08-31 14:46:15

C# string b

2009-08-31 14:56:32

C# Byte數(shù)組轉換

2009-09-02 10:58:02

C#動態(tài)數(shù)組

2009-09-03 17:18:40

C#擴展性對象模型

2009-09-18 10:58:31

C#數(shù)組操作

2009-09-23 09:36:34

C#數(shù)組

2009-08-26 10:09:52

byte常用擴展

2009-09-17 18:07:22

C#動態(tài)數(shù)組

2009-08-28 14:54:20

C# byte數(shù)組

2009-09-07 10:34:47

2009-08-13 13:03:52

C#結構體數(shù)組

2009-09-17 15:39:56

C#數(shù)組初始化

2009-08-14 17:45:52

C# ArrayLis

2009-08-17 18:34:50

C# ChangeCo
點贊
收藏

51CTO技術棧公眾號