日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#byte數(shù)組常用擴(kuò)展淺析

C# byte數(shù)組常用擴(kuò)展是我們編程中經(jīng)常會(huì)碰到的一些實(shí)用性很強(qiáng)的操作,那么C# byte數(shù)組常用擴(kuò)展都有哪些呢?下面將列出并用實(shí)例演示常用八種情況。

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、盤(pán)山網(wǎng)站維護(hù)、網(wǎng)站推廣。

C# byte數(shù)組常用擴(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 bytes)
  7. {
  8. var sb = new StringBuilder();
  9. foreach (byte b in bytes)
  10.  sb.Append(b.ToString("X2"));
  11. return sb.ToString();
  12.  }

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

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

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

C# byte數(shù)組常用擴(kuò)展應(yīng)用三:轉(zhuǎn)換為基礎(chǔ)數(shù)據(jù)類(lèi)型

 
 
 
  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類(lèi)還有很多方法(ToSingle、ToDouble、ToChar...),可以如上進(jìn)行擴(kuò)展。

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

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

C# byte數(shù)組常用擴(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);
  13. }

C# byte數(shù)組常用擴(kuò)展應(yīng)用六:位運(yùn)算

 
 
 
  1. //index從0開(kāi)始
  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;
  24.  }

C# byte數(shù)組常用擴(kuò)展應(yīng)用七:保存為文件

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

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

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

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


文章題目:C#byte數(shù)組常用擴(kuò)展淺析
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/cdosdpd.html