Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created July 18, 2011 23:11
Show Gist options
  • Save ecounysis/1090922 to your computer and use it in GitHub Desktop.
Save ecounysis/1090922 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def twodigits(st):
g = st[2:]
if (len(g) == 1):
g = '0' + g
return g.upper()
def n2h(n):
r = hex(n)
return twodigits(r)
def num2hex(red, green, blue):
return n2h(red) + n2h(green) + n2h(blue)
def hex2num(hexstr):
r = int(hexstr[0:2],16)
g = int(hexstr[2:4],16)
b = int(hexstr[4:6],16)
return (r,g,b)
def usage():
print "usage:"
print " n2h int -> hexstring"
print " num2hex int int int -> hexstring"
print " hex2num hexstring -> three ints"
def main():
import sys
try:
if (len(sys.argv) > 1):
command = sys.argv[1]
if (command == "n2h"):
print n2h(int(sys.argv[2]))
elif (command == "num2hex"):
print num2hex(int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))
elif (command == "hex2num"):
print hex2num(sys.argv[2])
else:
usage()
else:
usage()
except:
usage()
if (__name__ == "__main__"):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment