Skip to content

Instantly share code, notes, and snippets.

@svschannak
Last active January 3, 2016 21:09
Show Gist options
  • Save svschannak/8519344 to your computer and use it in GitHub Desktop.
Save svschannak/8519344 to your computer and use it in GitHub Desktop.
Create a valid mac-address from an string
def create_valid_mac_address(mac_address):
mac_address = mac_address.replace("-", "")
if len(mac_address) != 12:
raise InvalidMacAddressStringException("The String of the MAC-Address does not contain enough characters")
if not all(c in string.hexdigits for c in mac_address):
wrong_chars = ""
for c in mac_address:
if c not in string.hexdigits:
wrong_chars += "#%s#" % c
else:
wrong_chars += c
raise InvalidMacAddressStringException("The String contains characters, that are not hexadecimal: %s" % wrong_chars)
else:
mac_address = '-'.join(a+b for a,b in zip(mac_address[::2], mac_address[1::2]))
mac_address = mac_address.upper()
return mac_address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment