【C# / Extension】 扩展方法02 —— 扩展Byte

版权声明:本文为 ls9512 原创文章,转载请注明出处! https://blog.csdn.net/ls9512/article/details/79807303

C# 扩展方法系列

C# 扩展方法简介
C# 扩展方法01 —— 扩展 String & StringBuidler

扩展 Byte 的意义

byte 类型对于很多初学者来说,可能不是很常用,但在实际开发中,经常需要以字节的形式处理数据和文件,经常要对字节、字节数组、字节流进行繁琐的处理,扩展 byte 相关的方法,可以极大的提高实际开发的效率。

Byte 扩展

Hex 十六进制扩展

  • byte 转换为16进制
public static string ToHex(this byte b){
    return b.ToString("X2");
}
  • byte[] 转换为16进制
public static string ToHex(this IEnumerable<byte> bytes)
{
    var sb = new StringBuilder();
    foreach (var b in bytes)
    {
        sb.Append(b.ToString("X2"));
    }
    return sb.ToString();
}
  • byte[] 转换为带空格分割的16进制
public static string ToHexWithSpace(this IEnumerable<byte> bytes)
{
    var sb = new StringBuilder();
    foreach (var b in bytes)
    {
        sb.Append(b.ToString("X2"));
        sb.Append(" ");
    }
    return sb.ToString();
}

Base64 扩展

  • byte[] 转换为 Base64字符串
public static string ToBase64String(byte[] bytes) {
    return Convert.ToBase64String(bytes);
}

Convert 常规数据类型转换扩展

  • byte 转换为 int
public static int ToInt(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToInt32(bytes, startIndex);
}
  • byte 转换为 long
public static long ToLong(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToInt64(bytes, startIndex);
}
  • byte 转换为 char
public static char ToChar(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToChar(bytes, startIndex);
}
  • byte 转换为 float
public static float ToFloat(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToSingle(bytes, startIndex);
}
  • byte 转换为 double
public static double ToDouble(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToDouble(bytes, startIndex);
}
  • byte 转换为 boolen
public static bool ToBoolean(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToBoolean(bytes, startIndex);
}

String 转换扩展

  • byte 转换为 string
public static string ToString(this byte[] bytes, int startIndex = 0) {
    return BitConverter.ToString(bytes, startIndex);
}
  • 编码转换
public static string Decode(this byte[] bytes, Encoding encoding) {
    return encoding.GetString(bytes);
}

Hash 扩展

  • 使用指定算法计算Hash值
public static byte[] Hash(this byte[] bytes, string hashName = "ModSystem.Security.Cryptography.HashAlgorithm")
{
    var algorithm = string.IsNullOrEmpty(hashName) ? HashAlgorithm.Create() : HashAlgorithm.Create(hashName);
    return algorithm != null ? algorithm.ComputeHash(bytes) : null;
}

File 扩展

  • 保存为文件
public static void Save(this byte[] bytes, string path) {
    File.WriteAllBytes(path, bytes);
}

MemoryStream 扩展

  • 转换为 MemoryStream
public static MemoryStream ToMemoryStream(this byte[] bytes) {
    return new MemoryStream(bytes);
} 

Bitwise 位运算扩展

  • 判断第 index 位是否为1
public static bool GetBit(this byte b, int index) {
    return (b & (1 << index)) > 0;
}
  • 将 index 位设为 1
public static byte SetBit(this byte b, int index) {
    b |= (byte)(1 << index);
    return b;
}
  • 将 index 位设为 0
public static byte ClearBit(this byte b, int index) {
    b &= (byte)((1 << 8) - 1 - (1 << index));
    return b;
}
  • 将 index 位取反
public static byte ReverseBit(this byte b, int index) {
    b ^= (byte)(1 << index);
    return b;
}

猜你喜欢

转载自blog.csdn.net/ls9512/article/details/79807303