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

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

新聞中心

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

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

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

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

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

C# byte數(shù)組常用擴展應用三:轉(zhuǎn)換為基礎數(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ù)組常用擴展應用四:轉(zhuǎn)換為指定編碼的字符串

 
 
 
  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);
  13. }

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ù)組常用擴展應用八:轉(zhuǎn)換為內(nèi)存流

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

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


網(wǎng)站題目:C#byte數(shù)組常用擴展淺析
本文網(wǎng)址:http://m.5511xx.com/article/cdosdpd.html