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

巧思妙解byte常用擴展

開發(fā) 后端
本文介紹了byte常用擴展的8個應用,例如轉(zhuǎn)換為十六進制字符串,轉(zhuǎn)換為Base64字符串等。

本文介紹了byte常用擴展的8個應用,比較簡單,沒有太多技術(shù)含量,不用太多解釋,主要提供大家一個思路,如需要使用,請自行完善。

byte常用擴展應用一:轉(zhuǎn)換為十六進制字符串

  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常用擴展應用二:轉(zhuǎn)換為Base64字符串

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

byte常用擴展應用三:轉(zhuǎn)換為基礎(chǔ)數(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);  

BitConverter類還有很多方法(ToSingle、ToDouble、ToChar...),可以如上進行擴展。

byte常用擴展應用四:轉(zhuǎn)換為指定編碼的字符串

  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位設(shè)為1  
  8.  public static byte SetBit(this byte b, int index)  
  9.  {  
  10.     b |= (byte)(1 < <  index);  
  11.     return b;  
  12. }  
  13. //將第index位設(shè)為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常用擴展應用八:轉(zhuǎn)換為內(nèi)存流

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

【編輯推薦】

  1. 總結(jié)C#語言命名規(guī)范
  2. C#反射相關(guān)知識學習
  3. 大話F#和C#:是否會重蹈C#失敗的覆轍?
  4. 總結(jié)和學習C#接口
  5. 學習C#程序有感
責任編輯:book05 來源: cnblogs
相關(guān)推薦

2009-08-28 14:25:57

C# byte數(shù)組

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編輯器開發(fā)

2018-01-02 14:57:59

谷歌面試算法

2020-08-03 11:17:15

APP設(shè)計產(chǎn)品

2009-12-18 14:19:01

無線擴展通信技術(shù)特點

2024-03-11 10:19:30

Plasmo瀏覽器Web

2009-11-10 15:18:03

思科路由器常用配置命令

2009-11-26 14:40:51

無線路由器

2025-02-11 07:55:45

2015-11-02 09:16:08

2024-01-03 16:37:26

Jupyter工具開源

2009-01-03 08:56:00

局域網(wǎng)服務優(yōu)化

2023-06-12 07:00:40

Rust進度任務

2021-07-26 10:15:10

哈希字母異位詞

2011-07-13 09:59:55

點贊
收藏

51CTO技術(shù)棧公眾號