【分享】C# 字节帮助类 ByteHelper
生活随笔
收集整理的這篇文章主要介紹了
【分享】C# 字节帮助类 ByteHelper
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【分享】C#?字節幫助類 ByteHelper
獨立觀察員 2021 年 2 月 3 日
本文分享一個 C# 的字節(Byte)幫助類(ByteHelper),主要是一些字節、字節數組、十六進制、十六進制字符串等之間的轉換操作,適用場景包括但不限于對于 M1 卡區塊的讀寫時的數據轉換等操作。
代碼來源于網絡,本人整理重構、仔細閱讀代碼并添加了較為詳細的注釋,一切說明見代碼和注釋,就不再贅述了,有不對的地方歡迎大家指出。
下面就是全部代碼:
using System; using System.Collections.Generic; /** 源碼己托管: http://gitee.com/dlgcy/dotnetcodes*/ namespace DotNet.Utilities {/// <summary>/// 字節幫助類/// </summary>public class ByteHelper{/// <summary>/// 字節數組轉為 16 進制字符串/// </summary>/// <param name="bytes"> 字節數組 </param>/// <returns>16 進制字符串 </returns>public static string ToHexString(byte[] bytes){string hexString = string.Empty;for (int i = 0; i < bytes.Length; i++){hexString += ByteToHexStr(bytes[i]);}return hexString;}/// <summary>/// 字節轉為 16 進制字符串(一個字節轉為兩個字符:[0-F][0-F])/// </summary>/// <param name="inByte"> 字節 </param>/// <returns> 字符串 </returns>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;}/// <summary>/// 十六進制字符串轉為字節數組/// </summary>/// <param name="hexStr"> 十六進制字符串 </param>/// <returns> 字節數組 </returns>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];elserightPart = '0';int a = ByteToHexValue((byte)leftPart);int b = ByteToHexValue((byte)rightPart);// 由于 16 進制數的 ' 個位 ' 數只占 4bit,所以左部分左移 4 位,加上右部分,共占 8 位,即一個 byte;bytes[i] = (byte)((a << 4) + b);}return bytes;}/// <summary>/// 轉換字節(實際為英文 char)為 16 進制數據(0-15)/// </summary>/// <param name="b"> 字節 </param>/// <returns> 字節 </returns>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;}/// <summary>/// 區塊字符串數據轉為區塊字節數據(不足則補位:16 字節)/// </summary>/// <param name="blockData"> 區塊字符串數據 </param>/// <returns>List<byte></returns>public static List<byte> GetBlockBytes(string blockData){List<byte> blockBytes = new List<byte>();blockBytes.AddRange(HexStrToBytes(blockData));if (blockBytes.Count < 16){for (int i = blockBytes.Count; i < 16; i++){blockBytes.Add(0x00);}}return blockBytes;}} }?
感謝閱讀。
總結
以上是生活随笔為你收集整理的【分享】C# 字节帮助类 ByteHelper的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Core 5 在IIS,
- 下一篇: 如何在 ASP.Net Core 中使用