【分享】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]; 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; } /// <summary> /// 轉換位元組(實際為英文char)為16進制資料(0-15) /// </summary> /// <param name="b">位元組</param> /// <returns>位元組</returns> public static byte ByteToHexValue(byte b) { byte value = https://www.cnblogs.com/weiliuhong/p/0; if (b >= '0' && b <= '9') { //原值在ASCII中介于'0'-'9'之間的:減去0x30,即ASCII中'0'的十六進制表示(十進制為48),得到數值0-9, value = https://www.cnblogs.com/weiliuhong/p/(byte)(b - 0x30); } if (b >= 'A' && b <= 'F') { //原值在ASCII中介于'A'-'F'之間的:減去0x37,十進制為55,‘A’的ASCII十進制為65,所以可得到數值10-15, value = https://www.cnblogs.com/weiliuhong/p/(byte)(b - 0x37); } if (b >= 'a' && b <= 'f') { //原值在ASCII中介于'a'-'f'之間的:減去0x57,十進制為87,‘a’的ASCII十進制為97,所以可得到數值10-15, value = https://www.cnblogs.com/weiliuhong/p/(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 [http://dlgcy.com/csharp-bytehelper/]
微信訂閱號
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/256139.html
標籤:C#
上一篇:tcp通訊資料長度,資料的長度也要給回傳,要求是什么16位左低右高,請教
下一篇:使用C#實作資料結構堆
