日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
巧思妙解byte常用擴(kuò)展

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

創(chuàng)新互聯(lián)公司是專業(yè)的錦州網(wǎng)站建設(shè)公司,錦州接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行錦州網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

byte常用擴(kuò)展應(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();  

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

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

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

byte常用擴(kuò)展應(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)行擴(kuò)展。

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

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

byte常用擴(kuò)展應(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常用擴(kuò)展應(yīng)用六:位運(yùn)算

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

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

byte常用擴(kuò)展應(yīng)用八:轉(zhuǎn)換為內(nèi)存流

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

網(wǎng)站名稱:巧思妙解byte常用擴(kuò)展
地址分享:http://www.dlmjj.cn/article/dpdiccc.html