發布時間:2020-08-06 21:30 作者:獨孤劍 閱讀:2000
using System; using System.Collections.Generic; using System.Data; using System.Text; namespace ConsoleApp23 { class Program { static void Main(string[] args) { // 16進制字符串, 0x000000000002654D string str = "000000000002654D";// 或者 02654D // 16進制字符串 -> bytes byte[] a = BytesToHexString(str); // bytes -> 16進制字符串 string hex = BytesToHexString(a); // 轉換數值 long b = Convert.ToInt64(hex, 16); Console.Read(); } /// <summary> /// 16進制字符串轉byte數組 /// </summary> /// <param name="hexString">16進制字符</param> /// <returns></returns> public static byte[] BytesToHexString(string hexString) { // 將16進制秘鑰轉成字節數組 byte[] bytes = new byte[hexString.Length / 2]; for (var x = 0; x < bytes.Length; x++) { var i = Convert.ToInt32(hexString.Substring(x * 2, 2), 16); bytes[x] = (byte)i; } return bytes; } /// <summary> /// byte數組轉16進制字符串 /// </summary> /// <param name="bytes">byte數組</param> /// <returns></returns> public static string BytesToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.Length * 3); foreach (byte b in bytes) { sb.Append(Convert.ToString(b, 16).PadLeft(2, '0')); } return sb.ToString().ToUpper(); } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞