Last active
January 3, 2016 21:09
-
-
Save svschannak/8519344 to your computer and use it in GitHub Desktop.
Create a valid mac-address from an string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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