1

I want to build simple DHCP packet and one of the protocol oprion is client mac address (option 61), so i have my mac address:

string macAddress = "00:14:22:18:81:11";

and i want to put it in my packet array (6 bytes), this is what i have try and i wonder how to do that (i try to convert my string into byte[] but this array length is 24)

        // 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

1 Answer 1

2

Here is my proposal:

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);
Sign up to request clarification or add additional context in comments.

2 Comments

macAddress.Split(':').Select(b => Convert.ToByte(b)) gives errors for me when the string contains letters (A,B,C,D,E,F) :(
@Bosiwow Thanks for pointing this out, I forgot to specify base 16 for hex conversion. See edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.