user2280349 user2280349 - 6 months ago 22
Perl Question

Can SQLite output a packed integer?

Is there a way to have SQLite output an unsigned integer in a packed/binary format?

I'm looking to have SQLite produce the equivalent of the perl function "print pack('I', 1_234_567_890)" where every integer (less than 2^32) is output using exactly 4 bytes (well, on my linux box on intel hardware; I know this stuff varies by hardware and endian-ness.)

Is there some printf formatting magic that will get me what I'm looking for?

Answer

Big-endian, 32-bit value (l>):

char( (N >> 24) & 0xFF, (N >> 16) & 0xFF, (N >> 8) & 0xFF, N & 0xFF )

Little-endian, 32-bit value (l<):

char( N & 0xFF, (N >> 8) & 0xFF, (N >> 16) & 0xFF, (N >> 24) & 0xFF )
Comments