+ 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)

14th Mar 2017, 1:52 PM
Aya Habeeb
Aya Habeeb - avatar
8 ответов
+ 4
stanislav| yes dec variable stored a decimal number , i dont write it here 😅
14th Mar 2017, 5:06 PM
Aya Habeeb
Aya Habeeb - avatar
+ 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
14th Mar 2017, 5:53 PM
Aya Habeeb
Aya Habeeb - avatar
+ 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
14th Mar 2017, 5:27 PM
Aya Habeeb
Aya Habeeb - avatar
+ 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?
14th Mar 2017, 6:01 PM
Aya Habeeb
Aya Habeeb - avatar
+ 1
What is dec variable? it is not defined
14th Mar 2017, 5:01 PM
Stanislav Syarkevich
Stanislav Syarkevich - avatar
+ 1
Post full code, please :)
14th Mar 2017, 5:18 PM
Stanislav Syarkevich
Stanislav Syarkevich - avatar
+ 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
14th Mar 2017, 5:46 PM
Stanislav Syarkevich
Stanislav Syarkevich - avatar
+ 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! :)
14th Mar 2017, 5:55 PM
Stanislav Syarkevich
Stanislav Syarkevich - avatar