Reputation: 23
I have a variable holding x length number, in real time I do not know x. I just want to get divide this value into two. For example;
variable holds a = 01029108219821082904444333322221111
I just want to take last 16 integers as a new number, like
b = 0 # initialization
b = doSomeOp (a)
b = 4444333322221111 # new value of b
How can I divide the integer ?
Upvotes: 0
Views: 149
Reputation: 336158
>>> a = 1029108219821082904444333322221111
>>> a % 10**16
4444333322221111
or, using string manipulation:
>>> int(str(a)[-16:])
4444333322221111
If you don't know the "length" of the number in advance, you can calculate it:
>>> import math
>>> a % 10 ** int(math.log10(a)/2)
4444333322221111
>>> int(str(a)[-int(math.log10(a)/2):])
4444333322221111
And, of course, for the "other half" of the number, it's
>>> a // 10 ** int(math.log10(a)/2) # Use a single / with Python 2
102910821982108290
EDIT:
If your actual question is "How can I divide a string in half", then it's
>>> a = "\x00*\x10\x01\x00\x13\xa2\x00@J\xfd\x15\xff\xfe\x00\x000013A200402D5DF9"
>>> half = len(a)//2
>>> front, back = a[:half], a[half:]
>>> front
'\x00*\x10\x01\x00\x13¢\x00@Jý\x15ÿþ\x00\x00'
>>> back
'0013A200402D5DF9'
Upvotes: 8
Reputation: 3343
I would just explot slices here by casting it to a string, taking a slice and convert it back to a number.
b = int(str(a)[-16:])
Upvotes: 0