Decoding NumPy int64 binary representation
Question: So I did a stupid thing, and forgot to explicitly type-convert some values I
was putting into an SQLite database (using Python's SQLalchemy). The column
was set up to store an `INT`, whereas the input was actually a `numpy.int64`
dtype.

The values I am getting back out of the database look like:

`b'\x15\x00\x00\x00\x00\x00\x00\x00'`

It seems that SQLite has gone and stored the binary representation for these
values, rather than the integer itself.

Is there a way to decode these values in Python, or am I stuck with loading
all my data again (not a trivial exercise at this point)?


Answer: You can use
[`struct.unpack()`](https://docs.python.org/3/library/struct.html#struct.unpack):

    
    
    >>> import struct
    >>> value = struct.unpack('<q', b'\x15\x00\x00\x00\x00\x00\x00\x00')
    >>> value
    (21,)
    >>> value[0]
    21
    

That assumes that the data was stored little endian as specified by the `<` in
the `unpack()` format string, and that it is a signed "long long" (8 bytes) as
specified by the `q`. If the data is big endian:

    
    
    >>> struct.unpack('>q', b'\x15\x00\x00\x00\x00\x00\x00\x00')
    (1513209474796486656,)
    

I imagine that little endian is more likely to be correct in this case.

P.S. I have just confirmed that when a `numpy.int64` is inserted into a SQLite
`int` field it can be retrieved using `struct.unpack()` as shown above.

