Skip to content

Instantly share code, notes, and snippets.

@FlyingJester
Last active August 29, 2015 13:56
Show Gist options
  • Save FlyingJester/9146349 to your computer and use it in GitHub Desktop.
Save FlyingJester/9146349 to your computer and use it in GitHub Desktop.
[Python] Create a string that, when written to a binary file, represents a number. Also contains null-padding function for fixed-width representation.
def StringifyKeepingValue(s):
stringy = ""
while s:
digit = s %256
s-= s%256
s = s/256
stringy+=chr(digit)
return stringy
def PadString(val, s, l):
if len(s) == l:
return s
if len(s) > l:
return s [0:l]
while len(s) < l:
s=val+s
return s
def PadAndString(n, l):
return PadString(chr(0), StringifyKeepingValue(n), l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment