博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【分享】C# 字节帮助类 ByteHelper
阅读量:4036 次
发布时间:2019-05-24

本文共 3951 字,大约阅读时间需要 13 分钟。

【分享】C# 字节帮助类 ByteHelper

独立观察员 2021 年 2 月 3 日

本文分享一个 C# 的字节(Byte)帮助类(ByteHelper),主要是一些字节、字节数组、十六进制、十六进制字符串等之间的转换操作,适用场景包括但不限于对于 M1 卡区块的读写时的数据转换等操作。

代码来源于网络,本人整理重构、仔细阅读代码并添加了较为详细的注释,一切说明见代码和注释,就不再赘述了,有不对的地方欢迎大家指出。

下面就是全部代码:

using System;using System.Collections.Generic;/* * 源码己托管: http://gitee.com/dlgcy/dotnetcodes */namespace DotNet.Utilities{    ///     /// 字节帮助类    ///     public class ByteHelper    {        ///         /// 字节数组转为 16 进制字符串        ///         ///  字节数组         /// 
16 进制字符串
public static string ToHexString(byte[] bytes) { string hexString = string.Empty; for (int i = 0; i < bytes.Length; i++) { hexString += ByteToHexStr(bytes[i]); } return hexString; } /// /// 字节转为 16 进制字符串(一个字节转为两个字符:[0-F][0-F]) /// /// 字节 ///
字符串
public static string ByteToHexStr(byte inByte) { string result = string.Empty; try { char[] hexDict = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] charParts = new char[2]; charParts[0] = hexDict[(inByte >> 4) & 0x0F]; // 放在 byte 左半部分的重新移回右边并匹配十六进制字符; charParts[1] = hexDict[inByte & 0x0F]; // 放在 byte 右半部分的直接匹配十六进制字符; result = new string(charParts); } catch (Exception ex) { Console.WriteLine(ex); } return result; } /// /// 十六进制字符串转为字节数组 /// /// 十六进制字符串 ///
字节数组
public static byte[] HexStrToBytes(string hexStr) { /* 说明:1byte=8bit,中文 char=2byte (此处不考虑),英文 char=1byte, 1 个 “个位” 的十六进制数占 4bit,所以 2 个英文 char 转为十六进制数后占一个 byte */ byte[] bytes = new byte[hexStr.Length / 2 + (((hexStr.Length % 2) > 0) ? 1 : 0)]; for (int i = 0; i < bytes.Length; i++) { char leftPart = hexStr[i * 2]; char rightPart; if ((i * 2 + 1) < hexStr.Length) rightPart = hexStr[i * 2 + 1]; else rightPart = '0'; int a = ByteToHexValue((byte)leftPart); int b = ByteToHexValue((byte)rightPart); // 由于 16 进制数的 ' 个位 ' 数只占 4bit,所以左部分左移 4 位,加上右部分,共占 8 位,即一个 byte; bytes[i] = (byte)((a << 4) + b); } return bytes; } /// /// 转换字节(实际为英文 char)为 16 进制数据(0-15) /// /// 字节 ///
字节
public static byte ByteToHexValue(byte b) { byte value = 0; if (b >= '0' && b <= '9') { // 原值在 ASCII 中介于 '0'-'9' 之间的:减去 0x30,即 ASCII 中 '0' 的十六进制表示(十进制为 48),得到数值 0-9。 value = (byte)(b - 0x30); } if (b >= 'A' && b <= 'F') { // 原值在 ASCII 中介于 'A'-'F' 之间的:减去 0x37,十进制为 55,‘A’的 ASCII 十进制为 65,所以可得到数值 10-15。 value = (byte)(b - 0x37); } if (b >= 'a' && b <= 'f') { // 原值在 ASCII 中介于 'a'-'f' 之间的:减去 0x57,十进制为 87,‘a’的 ASCII 十进制为 97,所以可得到数值 10-15。 value = (byte)(b - 0x57); } return value; } /// /// 区块字符串数据转为区块字节数据(不足则补位:16 字节) /// /// 区块字符串数据 ///
List
public static List
GetBlockBytes(string blockData) { List
blockBytes = new List
(); blockBytes.AddRange(HexStrToBytes(blockData)); if (blockBytes.Count < 16) { for (int i = blockBytes.Count; i < 16; i++) { blockBytes.Add(0x00); } } return blockBytes; } }}

 

感谢阅读。

转载地址:http://isudi.baihongyu.com/

你可能感兴趣的文章
mac:移动python包路径
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
yuv420 format
查看>>
YUV420只绘制Y通道
查看>>
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt5 everywhere 编译summary
查看>>
qt5 everywhere编译完成后,找不到qmake
查看>>
arm-linux开机读取硬件时钟,设置系统时钟。
查看>>
交叉编译在x86上调试好的qt程序
查看>>
/dev/input/event0 键盘输入
查看>>
qt 创建异形窗体
查看>>
可重入函数与不可重入函数
查看>>