Created
May 21, 2015 21:49
-
-
Save benagricola/5d90fd329dd85ba3f8b6 to your computer and use it in GitHub Desktop.
BGP extended community number decoding - Python vs. Lua
This file contains 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
#!/usr/bin/env tarantool | |
local pickle = require('pickle') | |
local denumerate = function(num) | |
local bin = '' | |
while num > 0 do | |
local char = num % 256 | |
num = num - char | |
num = num / 256 | |
bin = string.char(char) .. bin | |
end | |
return bin | |
end | |
local num = 144678151251494874 | |
print(pickle.unpack('bbNn',denumerate(num))) | |
#2 2 201755 2016 -- WRONG :( |
This file contains 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
#!/usr/bin/env python | |
from struct import unpack | |
def denumerate(num): | |
dat = '' | |
while num > 0: | |
char = num % 256 | |
num = num - char | |
num = num / 256 | |
dat = chr(char) + dat | |
return dat | |
num = 144678151251494874 | |
print(unpack('!bblh',denumerate(num))) | |
#(2, 2, 201755, 2010) -- CORRECT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment