p

P

packed-binary.ss

format strings

( pack format v1 v2 ... )

Returns a byte string containing the values v1, v2, ... packed according to the given format . The arguments must match the values required by the format exactly.



( pack-into

format buffer offset v1 v2 ...

)

Pack the values v1, v2, ... according to the given format into the (mutable) byte string buffer starting at offset . Note that the offset is not an optional argument.



( write-packed format port v1 v2 ... )

Pack the values v1, v2, ... according to the given format and write them to the specified output port . Note that port is not an optional argument. [Note that write-packed is not part of the Python module, but was added because of its utility.]



( unpack format bytes )

Unpack the bytes according to the given format . The result is a list, even if it contains exactly one item. The bytes must contain exactly the amount of data required by the format [ (bytes-length bytes) must equal (calculate-size format) ].



( unpack-from format buffer (offset 0) )

Unpack the byte string buffer according to the given format . The result is a list, even if it contains exactly one element. The buffer must contain at least the amount of data required by the format [ (- (bytes-length buffer) offset) must be at least (calculate-size format) ].



( read-packed format port )

Read (calculate-size format ) bytes from the input port and unpack them according to the given format . The result is a list, even if it contains exactly one element. [Note that read-packed is not part of the Python module, but was added because of its utility.]

( calculate-size format )

Return the size of the byte string corresponding to the given format .



Format C Type PLT Scheme x pad byte no value c char char b signed char integer B unsigned char integer h short integer H unsigned short integer i int integer I unsigned int integer l long integer L unsigned long integer q long long integer Q unsigned long long integer f float real d double real s char[] string

"4h"

"hhhh"

"s"

"10s"

"10c"

"0s"

"0c"

Character Byte order Size and alignment @ native native = native standard < little endian standard > big endian standard ! network (= big endian) standard

"@"

short

int

long

long long

float

double

"@"

"="

"!"

">" .



Hint, to align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero. For example, the format "llh0l" specified two pad bytes at the end, assuming longs are aligned on 4-byte boundaries. This only works when native size and alignment are in effect; standard size and alignment does not enforce any alignment.



The current implementation may not properly handle native alignment in all cases. For the current implementation, the native alignment is assumed to be the same as the size. This may result in excess pad bytes, particularly for 8-byte objects.



I will be putting the code up on the Schematics project on source forge (probably tomorrow) and it will be available on PLaneT soon thereafter. " orHint, to align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero. For example, the formatspecified two pad bytes at the end, assuming longs are aligned on 4-byte boundaries. This only works when native size and alignment are in effect; standard size and alignment does not enforce any alignment.The current implementation may not properly handle native alignment in all cases. For the current implementation, the native alignment is assumed to be the same as the size. This may result in excess pad bytes, particularly for 8-byte objects.I will be putting the code up on the Schematics project on source forge (probably tomorrow) and it will be available on PLaneT soon thereafter. Labels: packed-binary