Home > Net >  Python - Same array, different bytes representation
Python - Same array, different bytes representation

Time:05-21

could anyone please help me understand why these 2 arrays are not the same:

Base64 arrays https://wetransfer.com/downloads/a24f099878ac87d204716d8f60f98bd720220519230008/a298acd8ad87d59e9b48501b918931c420220519230037/c84cbb

To load the array, execute:

import base64
import array
a1 = array.array('I', base64.b64decode(data_base64))

This method asserts both arrays are equal:

def assert_equal_arrays(a1, a2) -> None:
    if len(a1) != len(a2): 
        raise RuntimeError(f'Lengths are not same a1:{len(a1)}, a2:{len(a2)}')
    for i in range(0, len(a1) - 1):
        if (a1[i] != a2[i]):
            raise RuntimeError(f'Elements at {i} are not the equal, a1[{i}]:{a1[i]}, a2[{i}]:{a2[i]}')
        if (type(a1[i]) != type(a2[i])):
            raise RuntimeError(f'Elements at {i} are not the same type, type(a1[{i}]):{type(a1[i])}, a2[{i}]:{type(a2[i])}') 

It does not fail when I pass the array objects, however, it fails when I pass the bytes:

assert_equal_arrays(a1, a2) #Pass
assert_equal_arrays(a1.tobytes(), a2.tobytes()) #Fails

What am I missing?

Thanks in advance.

CodePudding user response:

It's because you have range(0, len(a1) - 1) in your loop.

It should be range(0, len(a1))

Also.. I just ran this

with open("../../../Downloads/result_db_array.txt") as f:
    a = f.read()
with open("../../../Downloads/full_db_array.txt") as ff:
    b = ff.read()
for i,x in enumerate(a):
    if b[i] != x:
        print(f"({i},{x},{b[i]})")

this was the output.

(11661818,S,T)

If their strings are not equal then the base64 arrays wouldn't be either.

  • Related