+ 4
How this code run to convert from decimal to hexa
s="" h=[str(i) fot i in range(10)]+["A","B","C","D","E","F"] while dec : s+=h[dec%16] dec//=16 hexsa = [::-1] print (hexsa)
8 Réponses
+ 4
stanislav| yes dec variable stored a decimal number , i dont write it here
😅
+ 4
yes you are right it should be // couse i wrote it in mobile i didn't pay attention .. the output true case 10000000 in binary equal 80 in hexa but 129 in decimal
+ 3
binary = input("Enter a digit in binary:")
dec=0
length = len (binary)-1
for i in binary :
integer = int(i)
dec += integer *2**length
length -=1
h = [ str (i) for i in range (10)] + [ "A","B","C","D","E","F"]
s=""
while dec:
s+=h[dec%16]
dec\\=16
hexa = s[::-1]
print(hexa)
these is the full code to convert from binary to hexa
+ 3
when i wrote the full code again , i mentioned that the code to convert from binary to hexa 😅 , i don't understand the while loop , can u help?
+ 1
What is dec variable? it is not defined
+ 1
Post full code, please :)
+ 1
I guess that the main mistake "\\" in dec\\=16 . Should be "//", no?
Enter a digit in binary:10000000
80
But logic of program is wrong, answer should be 127 as I remember in my example
+ 1
Sorry, it seems like it works fine, I just was confused, because topic sounds like How this code run to convert from decimal to hexa, but program asking for binary. So, it seems like it works ! Good job! :)