Skip to content

Instantly share code, notes, and snippets.

@benagricola
Created May 21, 2015 21:49
Show Gist options
  • Save benagricola/5d90fd329dd85ba3f8b6 to your computer and use it in GitHub Desktop.
Save benagricola/5d90fd329dd85ba3f8b6 to your computer and use it in GitHub Desktop.
BGP extended community number decoding - Python vs. Lua
#!/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 :(
#!/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