Question

I am creating and then writing data to a file (a new 'ESRI Shape file') using PHP, fopen, fseek, pack etc. The file spec is here http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.

The file spec states that the data written needs to be in a combination of the following:

  • Integer: Signed 32-bit integer (4 bytes) - Big Endian
  • Integer: Signed 32-bit integer (4 bytes) - Little Endian
  • Double: Signed 64-bit IEEE double-precision floating point number (8 bytes) - Little Endian

I cant seem to find a pack() format that allows for these formats. I don't want to use a machine dependent format as this code may be running on a variety of platforms.

Can anyone advise on what format (or combination of formats) I need to use for these 3 formats?

Many thanks, Steve

Was it helpful?

Solution

You could check the endianness of the machine running the code and reverse the bytes manually as necessary. The code below should work, but you will only be able to convert one int or float at a time.

define('BIG_ENDIAN', pack('L', 1) === pack('N', 1));

function pack_int32s_be($n) {
    if (BIG_ENDIAN) {
        return pack('l', $n); // that's a lower case L
    }
    return strrev(pack('l', $n));
}
function pack_int32s_le($n) {
    if (BIG_ENDIAN) {
        return strrev(pack('l', $n));
    }
    return pack('l', $n); // that's a lower case L
}
function pack_double_be($n) {
    if (BIG_ENDIAN) {
        return pack('d', $n);
    }
    return strrev(pack('d', $n));
}
function pack_double_le($n) {
    if (BIG_ENDIAN) {
        return strrev(pack('d', $n));
    }
    return pack('d', $n);
}

OTHER TIPS

If PHP doesn't support it, you could implement your own.

function pack_int32be($i) {
   if ($i < -2147483648 || $i > 2147483647) {
      die("Out of bounds");
   }
   return pack('C4',
      ($i >> 24) & 0xFF,
      ($i >> 16) & 0xFF,
      ($i >>  8) & 0xFF,
      ($i >>  0) & 0xFF
   );
}

function pack_int32le($i) {
   if ($i < -2147483648 || $i > 2147483647) {
      die("Out of bounds");
   }
   return pack('C4',
      ($i >>  0) & 0xFF,
      ($i >>  8) & 0xFF,
      ($i >> 16) & 0xFF,
      ($i >> 24) & 0xFF
   );
}

The double-precision LE is much harder. Supporting quad-precision system would involve packing the number using d, converting it to a binary string, splitting the binary into fields, truncating the fields to the right size if they're too large, concatenating the fields, then converting from binary to bytes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top