0

I have following simple (trivial) method in C#, which should return MAC Address of selected network interface card (NIC):

public byte[] GetMacAddress(int adapterIndex)
{
    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

    if ((adapterIndex >= networkInterfaces.Length) || (adapterIndex < 0))
    {
        return new byte[6] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    }
    else
    {
        return networkInterfaces[adapterIndex].GetPhysicalAddress().GetAddressBytes();
    }   // if
}   // GetMacAddress

Now, method is called from Program.cs (it's main method):

using System;
using Comms.NwStack.IpLayer.IpGateway.Ndis;

namespace NDISTester
{
    class Program
    {
        static void Main(string[] args)
        {
            TCP_AdapterList networkAdapters = new TCP_AdapterList();

            Console.WriteLine(networkAdapters.GetName(0) +
                              " " +
                              networkAdapters.GetMacAddress(0).ToString());
            Console.ReadKey();
        }   // Main
    }   // class
}   // namespace

and here is the output:

Local Area Connection  System.Byte[]

Why do I get empty MAC Address?

1
  • 1
    Every class in NET derives from the base class object who defines the ToString method. But the ToString method of the base class object cannot be accurate for every class. So if a class needs a precise ToString implementation it should override ToString with a specific implementation. If the class doesn't override ToString() then the base ToString will simply return the name of the class. The byte[] array has no override for ToString. Commented Jan 24, 2017 at 8:43

1 Answer 1

3

Byte array ToString method returns class name instead of it's data. BitConverter.ToString() to get byte[] data into string.

Use

BitConverter.ToString(networkAdapters.GetMacAddress(0));

instead of

networkAdapters.GetMacAddress(0).ToString()
Sign up to request clarification or add additional context in comments.

Comments

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.