C# 字符串转字节数组并输出16进制格式
string input = '114,86,92,93,94,90,93,0,101,66,89,90,87,124,89'; string[] parts = input.Split(','); byte[] ip = new byte[parts.Length];
for (int i = 0; i < parts.Length; i++) { ip[i] = Convert.ToByte(parts[i]); }
string output = 'var ip = new byte[] { '; for (int i = 0; i < ip.Length - 1; i++) { output += $'0x{ip[i]:X2}, '; } output += $'0x{ip[ip.Length - 1]:X2}, '; output += '};';
Console.WriteLine(output); // Output: var ip = new byte[] { 0x72, 0x56, 0x5C, 0x5D, 0x5E, 0x5A, 0x5D, 0x00, 0x65, 0x42, 0x59, 0x5A, 0x57, 0x7C, 0x59, };
原文地址: https://www.cveoy.top/t/topic/qoEK 著作权归作者所有。请勿转载和采集!