C# 如何发送已知 "77 85 25 63 82 15 74" 这样的16进制 ?标签:string,16,C#,strArr,UpdClient,Length,byte,data From: https://blog.51cto.com/51souta/5805159
public static byte[] HexStringToBytes(string hs)
{
string[] strArr = hs.Trim().Split(' ');
byte[] b = new byte[strArr.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < strArr.Length; i++)
{
b[i] = Convert.ToByte(strArr[i], 16);
}
//按照指定编码将字节数组变为字符串
return b;
}
调用的时候
直接
string sendCmdStr= "77 85 25 63 82 15 74";
byte[] data=HexStringToBytes(sendCmdStr);
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Parse(IP), port);
udpclient.Send(data, data.Length, ipendpoint);
udpclient.Close();