日期:2014-05-20  浏览次数:20849 次

string 与byte[]的问题在线等
string   value= "0x22,   0x25,   0xd3,   0xdd,   0xac,   0x34,   0x98,   0x2f,   0xa1,   0xa1,   0x52,   0xc9,   0x2c,   0xbc,   0x25,   0x66,   0x27,   0xa8,   0x34,   0x38,   0xa4,   0x91,   0x56,   0x89,   0x55,   0xee,   0xb6,   0x15,   0x3b,   0x12,   0x4f,   0xf4 "

现在想让他成为:
byte[]   value=={0x22,   0x25,   0xd3,   0xdd,   0xac,   0x34,   0x98,   0x2f,   0xa1,   0xa1,   0x52,   0xc9,   0x2c,   0xbc,   0x25,   0x66,   0x27,   0xa8,   0x34,   0x38,   0xa4,   0x91,   0x56,   0x89,   0x55,   0xee,   0xb6,   0x15,   0x3b,   0x12,   0x4f,   0xf4}

请问该如何转换。谢谢。



------解决方案--------------------
string[] Values = value.Split( ' ');
------解决方案--------------------
string[] Values = value.Split( ', ');应该是这个你试试
------解决方案--------------------
string value = "0x22, 0x25, 0xd3, 0xdd, 0xac, 0x34, 0x98, 0x2f, 0xa1, 0xa1, 0x52, 0xc9, 0x2c, 0xbc, 0x25, 0x66, 0x27, 0xa8, 0x34, 0x38, 0xa4, 0x91, 0x56, 0x89, 0x55, 0xee, 0xb6, 0x15, 0x3b, 0x12, 0x4f, 0xf4 ";
value = value.Replace( " ", " ");
string[] arr1 = value.Split( ', ');
int btl = arr1.Length;
byte[] bt = new byte[btl];
for (int ix = 0; ix < btl; ix++)
{
bt[ix] = Convert.ToByte(arr1[ix], 16);
}
------解决方案--------------------
static byte[] ConvertString(string val)
{
string[] ss = val.Split( ", ".ToCharArray());
System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();
foreach(string s in ss)
{
if(s!= ", "&&s!= " "&&s!= " ")
{
sc.Add(s);
}
}
byte[] buffer = new byte[sc.Count];
for(int i=0;i <sc.Count;i++)
{
int v = Convert.ToInt32(sc[i],16);
buffer[i]=byte.Parse(v.ToString());
}
return buffer;
}