62,267
社区成员
发帖
与我相关
我的任务
分享





string str = "4142434445464" ;//string str = "414243444546" ;
byte[] b = new byte[str.Length % 2 == 0 ? str.Length / 2 : str.Length / 2 + 1];
for (int i = 0; i < b.Length; i++)
{
b[i] = Convert.ToByte("0x" + str[2 * i].ToString() + (str.Length > 2 * i + 1 ? str[2 * i + 1].ToString() : ""), 16);
Response.Write(b[i]);
}
string str = "414243444546";
if (str.Length % 2 == 0)
{
byte[] result = new byte[str.Length / 2];
int j = 0;
for (int i = 0; i < str.Length; i += 2)
result[j++] = Convert.ToByte(new string(new char[] { '0', 'x', str[i], str[i + 1] }), 16);
}
string str = "414243444546";
if (str.Length % 2 == 0)
{
byte[] result = new byte[str.Length / 2];
int j = 0;
for (int i = 0; i < str.Length; i += 2)
result[j++] = (byte)Convert.ToInt16(new string(new char[] { '0', 'x', str[i], str[i + 1] }), 16);
}