我想构建简单的DHCP packet
,其中一个协议oprion是client mac address
(选项61),所以我有我的mac地址:
string macAddress = "00:14:22:18:81:11";
我希望把它放在我的数据包数组(6个字节)中,这是我试过的,我想知道该怎么做(我尝试将我的字符串转换为byte []但是这个数组长度是24)< / p>
// Set requested ip address - 61
index += DHCPMessageTypeLength;
packetArrayBytes[index] = 61; // option
packetArrayBytes[index + 1] = 7; // length
packetArrayBytes[index + 2] = 1; // hardware type Ethernet
packetArrayBytes[index + 3] = ?; // mac
packetArrayBytes[index + 4] = ?; // mac
packetArrayBytes[index + 5] = ?; // mac
packetArrayBytes[index + 6] = ?; // mac
packetArrayBytes[index + 7] = ?; // mac
packetArrayBytes[index + 8] = ?; // mac
答案 0 :(得分:2)
以下是我的建议:
List<byte> packet = new List<byte>();
packet.AddRange(new byte[] { 61, 7, 1 });
packet.AddRange(macAddress.Split(':').Select(b => Convert.ToByte(b, 16)));
Array.Copy(packet.ToArray(), 0, packetArrayBytes, DHCPMessageTypeLength, packet.Count);