日期:2014-05-17  浏览次数:20910 次

10进制 string数组转换成16进制byte数组
一个10进制string型数组{01,10,50,100,1000,5000},如何转换成16进制byte型{0x01,0x0A,0x32,0x64,0x03,0xE8,0x13,0x88}。
其中1000,5000转成16进制后是03E8和1388,拆成两个数。就是保证转换后每个数都是两位,说的够清楚了吧。
求各位大神解答

------解决方案--------------------
再拆分不就完了?
转换方法就这样的

1000.ToString("X2").PadLeft(4,'0').Substring(0,2)
1000.ToString("X2").PadLeft(4,'0').Substring(2,2)


------解决方案--------------------
C# code
string[] source = { "01", "10", "50", "100", "1000", "5000" };
byte[] result = source.Select(x => int.Parse(x)).SelectManay(x => x < 256 ? new byte[] { (byte)x } : new byte[] { (byte)(x / 256), (byte)(x % 256) }).ToArray();

------解决方案--------------------
C# code


String[] x = { "01", "10", "50", "100", "1000", "5000", int.MaxValue.ToString() };
String xxx = "";
foreach (String xx in x)
{ 
    String _x = int.Parse(xx).ToString("X");
    _x = _x.Length % 2 != 0 ? "0" + _x : _x;
    for (int i = 0; i < _x.Length / 2;i++ )
        xxx += "0x" + _x.Substring(i,2) + ",";        
}
xxx = xxx.TrimEnd(',');
String[] b = xxx.Split(',');;
foreach(string y in b)
    MessageBox.Show(y + "\r\n");