Issue
Let's say I have the
array = [1,2,3,4]
What I want is NOT to convert to the number 1234; but to take the bits of 1, 2, 3 and 4, concatenate them and convert back to a number.
In other words, I would have to perhaps convert each number/digit to binary, concatenate them and then convert back to a number.
Therefore, 1,2,3,4 would be 00000001
, 00000010
, 00000011
, 00000100
respectively. Concatenating them would lead to 00000001000000100000001100000100
which converted to an unsigned int would be 16909060
Keep in mind that the digits from the array come from ord(characters)
, so they should be 8bits in length, therefore concatenated should lead to a 32bit number
How would I do that?
Solution
In this simple case perhaps this suffices:
result = array[0] << 24 | array[1] << 16 | array[2] << 8 | array[3]
For example:
array = [1, 2, 3, 4]
result = array[0] << 24 | array[1] << 16 | array[2] << 8 | array[3]
print result
Prints this:
16909060
Answered By - Elektito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.