Is this a secure way of converting an array of 8 8bit integers into 1 64bit integer

Viewed 37

I am handling arrays of 8bit integers that looks like this:

ph0 = [[255, 0, 65, 165, 23, 32, 198, 43]]

I want to convert the array into an integer such that the binary representation of the elements in the array concatenated will match that of the integer.

So using the above example, its binary representation will be:

['0b11111111', '0b00000000', '0b01000001', '0b10100101', '0b00010111', '0b00100000', '0b11000110', '0b00101011']

In the end I want an integer whose binary representation will be all of those binary values concatenated together. Like this:

0b1111111100000000010000011010010100010111001000001100011000101011

This is what I have come up with:

def convertArrayToInt(hashArr):
    val = 0
    for i in range(len(hashArr[0])-1, -1, -1):
        val += (hashArr[0][i])*2**((7-i)*8)
    return val

I can faithfully assume that each slot does not exceed 255 (hence do not need to worry about a slot occupying more than 8 bits).

Is my implementation safe to use? Are there any better ideas?

1 Answers

You have a list of int objects. But this can be simply accomplished using:

int.from_bytes(bytes([255, 0, 65, 165, 23, 32, 198, 43]), 'big')
Related