In Python, I am aware certain methods exist in python 3 that can create a new integer from an existing byte array. However, I am looking for a way to create a reference to a byte array, as an integer. Such that, if the reference is changed, then the underlying byte array is also changed.
In C, this would be done like the following:
int main(void) {
unsigned char bytes[4] = {1, 0, 0, 0};
int* int_ref = (int*)bytes;
*int_ref += 59;
printf("bytes is now %u %u %u %u\n",
bytes[0],
bytes[1],
bytes[2],
bytes[3]);
return 0;
}
The above program prints 60. I am looking for a way to do this in Python.
ctypesmodule to approximate what you are looking for, this screams XY problem to me. What is it that you are actually trying to accomplish? Why can't you just use thebytearraywhen you need an integer? Note,arr[i]will return aninttype (which gets created on the fly).structmodule may be more what i am looking for. I guess the reference idea might not be doable for python.intobject that is aliased by achar *, not the other way around.