zoukankan      html  css  js  c++  java
  • c# 扩展方法奇思妙用基础篇三:byte 常用扩展

    转换为十六进制字符串

     

    复制代码
     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();
    12     }
    复制代码

     第二个扩展返回的十六进制字符串是连着的,一些情况下为了阅读方便会用一个空格分开,处理比较简单,不再给出示例。

    转换为Base64字符串

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

    转换为基础数据类型 

    复制代码
    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...),可以如上进行扩展。

    转换为指定编码的字符串 

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

    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     }
    复制代码

    位运算

    复制代码
     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     }
    复制代码

    保存为文件 

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

    转换为内存流 

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

    小结

      能想到的就这么多了!欢迎大家补充!

  • 相关阅读:
    Notes | 基于医疗知识图谱的问答系统实践
    Notes | 知识图谱介绍与Neo4J实战
    从jvm源码看synchronized
    Kakfa基础
    volatile
    JVM垃圾收集器
    原码,反码,补码
    mini设计模式
    xshell提示采购解决方法
    应试必备算法
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/3128739.html
Copyright © 2011-2022 走看看