From: Phrogz Date: 2007-06-19T07:35:05+09:00 Subject: Unpacking signed shorts and integers with specified endianness I'm deciphering a byte-based binary file structure which can be stored in either big or little endian. (The first byte of the file describes the format.) Among others, some of the fields are UINT32 (unsigned 4- byte integers), and some are INT32 (signed 4-byte integers). I know about BitStruct, but was trying to roll my own solution using String.unpack. (The file format has nested and repeating sections in it.) Given that I know exactly how many bytes I want for each field and whether the file is big or little endian, I thought it would be easy to pick a specific unpack character for each field. However, reading through the String.unpack docs, I can't find something that corresponds to signed 2-byte and 4-byte integers for a given endianness. Here's what I see (ASCII art ahead): | signed | unsigned | bytes | big | little | big | little | ------+-------+--------+-------+--------+ 1 | c | C | 2 | ? | ? | n | v | 4 | ? | ? | N | V | 1) I see "l" (lowercase L) which is 4 bytes treated as a signed integer...but in 'native' endian order. Does String.unpack not provide a way to unpack a 4-byte signed integer with a specified endianness? 2) I see "s" which is 2 bytes treated as a signed integer...but in 'native' endian order. Does String.unpack not provide a way to unpack a 2-byte signed integer with a specified endianness? (The file format doesn't actually use any signed 2-byte integers, but I wanted to include them for completeness.)