【发布时间】:2019-08-12 00:48:11
【问题描述】:
我在将二进制数字转换为十六进制的程序中遇到了另一个问题。我的程序运行良好,但以小型大写字母显示十六进制数字,但答案必须是大写字母,如question and sample run
所示这是我的代码
def binaryToHex(binaryValue):
#convert binaryValue to decimal
decvalue = 0
for i in range(len(binaryValue)):
digit = binaryValue.pop()
if digit == '1':
decvalue = decvalue + pow(2, i)
#convert decimal to hexadecimal
hexadecimal=hex(decvalue)
return hexadecimal
def main():
binaryValue = list(input("Input a binary number: "))
hexval=binaryToHex(binaryValue)
hexa=h1.capitalize() #Tried to use capitalize() function but didn't worl
print("The hex value is",hexa[ 2:4]) #strips off the first 2 digits
main()
【问题讨论】:
-
使用
upper而不是capitalize。 -
umm
hexa[ 2:4]不会只是去掉前两位数字...那就是[2:]...虽然这是某种练习,你不能只使用:format(int(binaryValue, 2), 'X')? -
@JonClements 我认为对此没有任何限制。我可以使用它
标签: python python-3.x hex