MacAddress

The MacAddress type represents a 48-bit MAC Address 1 as per the IEEE EUI standard 3. It serves the purpose of a Networking Adjacent worker class, and as a handy way to represent, store, format, and compare MAC addresses.

The MacAddress class implements IEquatable<MacAddress>, IComparable<MacAddress>, IComparable, IFormattable, and ISerializable.

Note

Unless otherwise stated recognized readable MAC Address formats include only the following formats:

  • IEEE 802 format for printing EUI-48 and MAC-48 addresses in six groups of two hexadecimal digits, separated by a dash (-). E.g. AA-BB-CC-DD-EE-FF

  • Common Six groups of two hexadecimal digits separated by colons (:). E.g. AA:BB:CC:DD:EE:FF

  • Six groups of two hexadecimal digits separated by a space character. E.g. AA BB CC DD EE FF

  • 12 hexadecimal digits with no delimitation. E.g. AABBCCDDEEFF

  • Cisco three groups of four hexadecimal digits separated by dots (.). E.g. AABB.CCDD.EEFF

For the sake of parsing and reading these formats are case insensitive.

MAC-48 Address.svg This file is licensed under the Creative Commons Attribution-Share Alike 2.5 Generic, 2.0 Generic and 1.0 Generic license.

Structure of a MAC-48 Address

Creation

Constructor

IEnumerable<byte>

A new MacAddress may be constructed by providing an IEnumerable<byte> of six bytes to the constructor.

public MacAddress(IEnumerable<byte> bytes)

Factory

Parse string

A MacAddress may also be created via either the Parse or safe TryParse method. Not that these methods are strict in that they will only succeed with a MAC address in a known format. If you wish to more liberally parse a string into a MacAddress see the ParseAny and TryParseAny defined below.

public static MacAddress Parse(string input)
public static bool TryParse(string input, out MacAddress macAddress)

ParseAny string

ParseAny and the safe TryParseAny allow the parsing of an arbitrary string that may be a Mac address into a MacAddress. It looks for six hexadecimal digits within the string, joins them and interprets the result as consecutive big-endian hextets. If six, and only six, hexadecimal digits are not found the parse will fail.

public static MacAddress ParseAny(string input)
public static bool TryParseAny(string input, out MacAddress macAddress)

Functionality

Properties

bool IsDefault

returns true if, and only if, the MAC Address is the EUI-48 default 2, meaning all bits of the MAC Address are set making it equivalent to FF:FF:FF:FF:FF:FF.

bool IsGloballyUnique

returns true if, and only if, is globally unique (OUI 4 enforced).

bool IsLocallyAdministered

returns true if, and only if, is locally administered.

bool IsMulticast

returns true if, and only if, the MAC Address is multicast.

bool IsUnicast

returns true if, and only if, the MAC Address is unicast.

bool IsUnusable

returns true if, and only if, the MAC Address is “unusable”, meaning all OUI bits of the MAC Address are unset.

MacAddress DefaultMacAddress

Provides a MacAddress that represents the default or null case MAC address.

Regex AllFormatMacAddressRegularExpression

Returns a regular expression for matching accepted MAC Address formats.

Regex CommonFormatMacAddressRegularExpression

Returns a regular expression for matching the “common” six groups of two uppercase hexadecimal digits format.

string AllFormatMacAddressPattern

Returns a regular expression pattern for matching accepted MAC Address formats.

string CommonFormatMacAddressPattern

Returns a regular expression pattern for matching the “common” six groups of two uppercase hexadecimal digits format.

Methods

GetAddressBytes

GetAddressBytes returns a copy of the underlying big-endian bytes of the MacAddress. This will always be six bytes in length.

public byte[] GetAddressBytes()

GetOuiBytes

GetOuiBytes returns the Organizationally Unique Identifier (OUI) 4 of the MAcAddress.

public byte[] GetOuiBytes()

GetCidBytes

GetCidBytes returns the Company ID (CID) 5 of the MAcAddress.

public byte[] GetCidBytes()

IFormatable

MacAddress offers a number or preexisting formats that are accessible via the standard ToString method provided by IFormattable interface.

Subnet format values

Format

Name

Description

Example

g

General Format

Uppercase hexadecimal encoded bytes separated by colons

AA:BB:CC:DD:EE:FF

H

Uppercase Hexadecimal

Contiguous uppercase hexadecimal digits

AABBCCDDEEFF

h

Lowercase Hexadecimal

Contiguous lowerrcase hexadecimal digits

aabbccddeeff

c

Cisco

Three groups of four uppercase hexadecimal digits separated by periods

AAAA.BBBB.CCCC

s

Space delimited hextets

Hexadecimal bytes separated by a space character

AA BB CC DD EE FF

d

IEEE 802

Hexadecimal encoded bytes separated by a dash characte

AA-BB-CC-DD-EE-FF

i

Integer

Big-endian integer value

187723572702975

Operators

MacAddress implements all the standard C# equality and comparison operators. The comparison operators treat the MacAddress bytes as an unsigned big-endian integer value.

Footnotes

1

48-Bit MAC is a Media Access Control Address (MAC) following both the now deprecated MAC-48 and the active EUI-48 specifications.

2

The recommended null or default value for EUI-48 is FF-FF-FF-FF-FF-FF

3

Guidelines for Use of Extended Unique Identifier (EUI), Organizationally Unique Identifier (OUI), and Company ID (CID)

4(1,2)

Organizationally Unique Identifier (OUI) is the first 3-bytes (24-bits) of a MAC-48 MAC Address.

5

Company Id (Cid) is the last 3-bytes (24-bits) of a MAC-48 MAC Address.

6

Usable EUI-48 values are based on a zeroed OUI. A all zero EUI value, such as 00-00-00-00-00-00 and 00-00-00-FA-BC-21, according to spec shall not be used as an identifier.