Byte陣列和十六進位制互轉操作
阿新 • • 發佈:2022-03-07
一、Byte 陣列轉十六進位制字串
/// <summary> /// Byte 陣列轉十六進位制字串 /// </summary> /// <param name="Bytes"></param> /// <returns></returns> public static string ByteToHex(byte[] Bytes) { string str = string.Empty; foreach (byte Byte in Bytes) { str += String.Format("{0:X2}", Byte) + " "; } return str.Trim(); }
二、字串轉十六進位制Byte陣列
/// <summary> /// 字串轉十六進位制Byte陣列 /// </summary> /// <param name="hexString"></param> /// <returns></returns> public static byte[] strToToHexByte(string hexString) { try { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } catch { return null; } }
本文來自部落格園,作者:農碼一生,轉載請註明原文連結:https://www.cnblogs.com/wml-it/p/15976903.html
技術的發展日新月異,隨著時間推移,無法保證本部落格所有內容的正確性。如有誤導,請大家見諒,歡迎評論區指正!
個人開原始碼連結,歡迎點亮:
GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
進開發學習交流群: