Skip to content

Instantly share code, notes, and snippets.

@lunhg
Created March 31, 2020 20:07
Show Gist options
  • Save lunhg/741dd56bf24cc85966b86ef5128eb2b4 to your computer and use it in GitHub Desktop.
Save lunhg/741dd56bf24cc85966b86ef5128eb2b4 to your computer and use it in GitHub Desktop.
Exercicio para converter numero decimal em binário
# Usage: converte-binario.py [OPTIONS, [ARGS]]
# converte um valor decimal para binario
#
# Options:
# --version show program's version number and exit
# -h, --help show this help message and exit
# -d DECIMAL, --decimal=DECIMAL
# PROGRAMA PRINCIPAL
PROG = "converte-binario"
VERSION = "0.0.1"
description = "converte um valor decimal para binario"
parser = OptionParser(usage='usage: %prog [OPTIONS, [ARGS]]',
version='%s %s' % (PROG, VERSION),
description=description)
parser.add_option("-d", "--decimal", action=None, help="Valor decimal")
(options, args) = parser.parse_args()
value = int(options.decimal)
tmp = []
while(value > 0):
bit = str(value % 2)
value = int(value/2)
tmp.append(bit)
i = len(tmp) - 1
result = []
while(i >= 0):
result.append(tmp[i])
i = i - 1
print("".join(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment