Skip to content

Instantly share code, notes, and snippets.

@lamoni
Last active February 27, 2024 22:35
Show Gist options
  • Save lamoni/bdf9a1bf3cd04a0cfacad3403fde8802 to your computer and use it in GitHub Desktop.
Save lamoni/bdf9a1bf3cd04a0cfacad3403fde8802 to your computer and use it in GitHub Desktop.
Convert 4 byte ASN (ASPLAIN) to ASDOT Notation and vice versa with Python
# e.g. Convert 4261160822 to 65020.10102
def convert_ASPLAIN_to_ASDOT(as_number):
if not(isinstance(as_number, int)) or as_number < 0 or as_number > 4294967295:
raise Exception('Invalid AS Number: must be int, must be greater than or equal to 0 and less than or equal to 4294967295')
return '{}.{}'.format(as_number // 65536, as_number % 65536)
# e.g. Convert 65020.10102 to 4261160822
def convert_ASDOT_to_ASPLAIN(as_dot_number):
if not(isinstance(as_dot_number, str)) or as_dot_number.count('.') != 1:
raise Exception('Invalid Dot Notation AS Number: Must be string and must have a single period')
(first,second) = [int(x) for x in as_dot_number.split('.')]
if (first < 0 or first > 65535) or (second < 0 or second > 65535):
raise Exception('Invalid Dot Notation AS Number: Both integers must be greater than 0 and less than 65536')
return (int(first) * 65536) + int(second)
print("3 = " + convert_ASPLAIN_to_ASDOT(3))
print("4261160822 = " + convert_ASPLAIN_to_ASDOT(4261160822))
print("4294967295 = " + convert_ASPLAIN_to_ASDOT(4294967295))
print("")
print("0.3 = " + str(convert_ASDOT_to_ASPLAIN('0.3')))
print("65020.10102 = " + str(convert_ASDOT_to_ASPLAIN('65020.10102')))
print("65535.65535 = " + str(convert_ASDOT_to_ASPLAIN('65535.65535')))
@lamoni
Copy link
Author

lamoni commented Oct 29, 2020

Output from script:

3 = 0.3
4261160822 = 65020.10102
4294967295 = 65535.65535

0.3 = 3
65020.10102 = 4261160822
65535.65535 = 4294967295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment