博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

将字符串的16进制转换成byte[]

Posted on 2011-11-18 20:35  Wuqh  阅读(506)  评论(0编辑  收藏  举报

 

        ///<summary>
/// 字符串形式的十六进制转换成byte[]
///</summary>
///<param name="str"></param>
///<returns></returns>
private byte[] StringToHex(string str)
{
string[] strs = str.Split('');
byte[] bytes = new byte[strs.Length];
int index = 0;
foreach (string s in strs)
{
int hex = Convert.ToInt32(s, 16);
bytes[index++] = (byte)hex;
}
return bytes;
}

  将测试数据包如5e 13 30 30 30 30 30 30 34 34 30 31 0b 20 30 35 30 32 31 31 30 34 35 35 5e 这样的原始数据发送出去

转换后