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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ls9512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值