成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

巧思妙解byte常用擴展

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

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

byte常用擴展應(yīng)用一:轉(zhuǎn)換為十六進(jì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();  

第二個擴展返回的十六進(jìn)制字符串是連著的,一些情況下為了閱讀方便會用一個空格分開,處理比較簡單,不再給出示例。

byte常用擴展應(yīng)用二:轉(zhuǎn)換為Base64字符串

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

byte常用擴展應(yīng)用三:轉(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...),可以如上進(jìn)行擴展。

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

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

byte常用擴展應(yīng)用五: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.  //使用默認(rèn)算法Hash  
  10. public static byte[] Hash(this byte[] data)  
  11. {  
  12.     return Hash(data, null);  

byte常用擴展應(yīng)用六:位運算

  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常用擴展應(yīng)用七:保存為文件

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

byte常用擴展應(yīng)用八:轉(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)知識學(xué)習(xí)
  3. 大話F#和C#:是否會重蹈C#失敗的覆轍?
  4. 總結(jié)和學(xué)習(xí)C#接口
  5. 學(xué)習(xí)C#程序有感
責(zé)任編輯:book05 來源: cnblogs
相關(guān)推薦

2009-04-27 21:28:56

2009-08-28 14:25:57

C# byte數(shù)組

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ā)

2020-08-03 11:17:15

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

2018-01-02 14:57:59

谷歌面試算法

2009-12-18 14:19:01

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

2024-03-11 10:19:30

Plasmo瀏覽器Web

2009-11-26 14:40:51

無線路由器

2009-11-10 15:18:03

思科路由器常用配置命令

2015-11-02 09:16:08

2025-02-11 07:55:45

2009-01-03 08:56:00

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

2024-01-03 16:37:26

Jupyter工具開源

2022-05-17 07:26:33

動畫CSS前端

2021-07-26 10:15:10

哈希字母異位詞

2011-07-13 09:59:55

點贊
收藏

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

主站蜘蛛池模板: 亚洲精品日韩综合观看成人91 | 中文一级片 | 在线免费毛片 | 欧美日韩国产高清 | 天天弄 | 亚洲国产成人av好男人在线观看 | 欧美性乱 | 国产羞羞视频在线观看 | 亚洲国产区| 一区在线观看视频 | 国产色婷婷精品综合在线手机播放 | 欧美成人a∨高清免费观看 色999日韩 | 亚洲国产情侣 | 91在线资源 | 午夜国产羞羞视频免费网站 | 婷婷色在线播放 | xxxcom在线观看 | 欧美在线一二三 | 国产成人精品一区 | 日本午夜在线视频 | 男人av网 | 久久久国产精品 | 一区二区在线免费观看视频 | 日韩中文在线视频 | 丁香五月网久久综合 | 草樱av| 黄色大片免费网站 | 精品在线一区 | 久久99精品视频 | 亚洲激情综合 | 97精品超碰一区二区三区 | 日韩精品免费 | 国产亚洲精品久久久久久豆腐 | 国产在线第一页 | 欧美一区 | 国产综合视频 | 最新伦理片| 日韩精品视频中文字幕 | 亚洲欧美日韩久久久 | 国产91精品久久久久久久网曝门 | 成人在线视频观看 |