C# 字符串 16進制 轉換
發布時間:2018-02-03 16:59
作者:獨孤劍
閱讀:1411
static void Main(string[] args)
{
byte[] bt = new byte[] { 0, 0, 0, 0, 0, 0, 7, 211 };
string str1 = "0x" + BitConverter.ToString(bt).Replace("-", "");// 0x00000000000007D3
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 8; i++)
{
//sb.Append(bt[i].ToString("X2"));
sb.AppendFormat("{0:X2}", bt[i]);
}
string str2 = "0x" + sb.ToString();// 0x00000000000007D3
int d = "0x00000000000007D3".Length;
//byte bt2 = Convert.ToByte("00000000000007D3");
//byte[] b2= StringToHexByte("00000000000007D3");
//byte[] bt3 = System.Text.Encoding.Default.GetBytes("00000000000007D3");//字符轉換成字節數組
string cd = 2651.ToString("X16");
long f = 0x00000000000017A7;
long g = int.Parse("00000000000017A7", System.Globalization.NumberStyles.AllowHexSpecifier);
}
/// <summary>
/// 字符串轉16進制字節數組
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static byte[] StringToHexByte(string input)
{
input = input.Replace(" ", "");
if ((input.Length % 2) != 0)
input += " ";
byte[] result = new byte[input.Length / 2];
for (int i = 0; i < result.Length; i++)
result[i] = Convert.ToByte(input.Substring(i * 2, 2), 16);
return result;
}
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞