Reputation: 48
I have been trying to write a small python script for the sha1 algorithm. I needed to initialize the global variables of h0 to h4.
After initalize the value of the integers, it changes to a different int for h0 and h3 values, and not h1, h2 or h4. I'm not sure why, as a quick google search shows that h0 and h3 are not special characters.
Relevant code:
h0 = 0
h1 = 0
h2 = 0
h3 = 0
h4 = 0
#Initialize some variables
def init_var():
global h0
global h1
global h2
global h3
global h4
h0 = 01100111010001010010001100000001
h1 = 11101111110011011010101110001001
h2 = 10011000101110101101110011111110
h3 = 00010000001100100101010001110110
h4 = 11000011110100101110000111110000
print "\n\n\n h0", h0
print "h1", h1
print "h2", h2
print "h3", h3
print "h4", h4
When I print it in the console, it gives:
robert@robert-VirtualBox:~/sha1-hash-algorithm$ python testalgorithm.py
h0 1393027350754575011593322497
h1 11101111110011011010101110001001
h2 10011000101110101101110011111110
h3 19342823492383875910635592
h4 11000011110100101110000111110000
Can someone tell me why is this happening? Is this some special character in python?
I have included a screen shot below of the output:
Upvotes: 0
Views: 78
Reputation: 1362
@luoluo has told you the reason. So If you want to assign a binary number to a variable you should use the prefix: 0b
>>> a = 0b10010
>>> print(a)
18
>>> a = 0b001010
>>> print(a)
10
And you should know that when you print a binary number to the console, Python will show a decimal number automatically.
Upvotes: 1
Reputation: 5533
In python numbers start with 0
are in base 8(octal numbers).
>>> 011
9
011
equals 1 * (8**1) + 1 * (8**0)
= 9
0111
equals 1 * (8**1) + 1 * (8**1) + 1 * (8**0)
= 73
In your case, it works as expected.
>>> h0 = 01100111010001010010001100000001
>>> h0
>>> 1393027350754575011593322497L
Upvotes: 3