Class PelConvert

Description

Conversion functions to and from bytes and integers.

The functions found in this class are used to convert bytes into integers of several sizes (bytesToShort, bytesToLong, and bytesToRational) and convert integers of several sizes into bytes (shortToBytes and longToBytes).

All the methods are static and they all rely on an argument that specifies the byte order to be used, this must be one of the class constants LITTLE_ENDIAN or BIG_ENDIAN. These constants will be referred to as the pseudo type PelByteOrder throughout the documentation.

Located in /src/PelConvert.php (line 56)


	
			
Class Constant Summary
 BIG_ENDIAN = false
 LITTLE_ENDIAN = true
Method Summary
static int bytesToByte (string $bytes, int $offset)
static void bytesToDump (string $bytes, [int $max = 0])
static int bytesToLong (string $bytes, int $offset, PelByteOrder $endian)
static array bytesToRational (string $bytes, int $offset, PelByteOrder $endian)
static int bytesToSByte (string $bytes, int $offset)
static int bytesToShort (string $bytes, int $offset, PelByteOrder $endian)
static int bytesToSLong (string $bytes, int $offset, PelByteOrder $endian)
static array bytesToSRational (string $bytes, int $offset, PelByteOrder $endian)
static int bytesToSShort (string $bytes, int $offset, PelByteOrder $endian)
static string longToBytes (int $value, PelByteOrder $endian)
static string shortToBytes (int $value, PelByteOrder $endian)
static string sLongToBytes (int $value, PelByteOrder $endian)
static string sShortToBytes (int $value, PelByteOrder $endian)
Methods
static method bytesToByte (line 195)

Extract an unsigned byte from a string of bytes.

  • return: the unsigned byte found at offset, e.g., an integer in the range 0 to 255.
static int bytesToByte (string $bytes, int $offset)
  • string $bytes: the bytes.
  • int $offset: the offset. The byte found at the offset will be returned as an integer. The must be at least one byte available at offset.
static method bytesToDump (line 378)

Format bytes for dumping.

This method is for debug output, it will format a string as a hexadecimal dump suitable for display on a terminal. The output is printed directly to standard out.

static void bytesToDump (string $bytes, [int $max = 0])
  • string $bytes: the bytes that will be dumped.
  • int $max: the maximum number of bytes to dump. If this is left out (or left to the default of 0), then the entire string will be dumped.
static method bytesToLong (line 285)

Extract an unsigned long from bytes.

  • return: the unsigned long found at offset, e.g., an integer in the range 0 to 4294967295.
static int bytesToLong (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The long found at offset will be returned as an integer. There must be at least four bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method bytesToRational (line 338)

Extract an unsigned rational from bytes.

  • return: the unsigned rational found at offset, e.g., an array with two integers in the range 0 to 4294967295.
static array bytesToRational (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The rational found at offset will be returned as an array. There must be at least eight bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method bytesToSByte (line 212)

Extract a signed byte from bytes.

  • return: the signed byte found at offset, e.g., an integer in the range -128 to 127.
static int bytesToSByte (string $bytes, int $offset)
  • string $bytes: the bytes.
  • int $offset: the offset. The byte found at the offset will be returned as an integer. The must be at least one byte available at offset.
static method bytesToShort (line 236)

Extract an unsigned short from bytes.

  • return: the unsigned short found at offset, e.g., an integer in the range 0 to 65535.
static int bytesToShort (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The short found at the offset will be returned as an integer. There must be at least two bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method bytesToSLong (line 314)

Extract a signed long from bytes.

  • return: the signed long found at offset, e.g., an integer in the range -2147483648 to 2147483647.
static int bytesToSLong (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The long found at offset will be returned as an integer. There must be at least four bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method bytesToSRational (line 359)

Extract a signed rational from bytes.

  • return: the signed rational found at offset, e.g., an array with two integers in the range -2147483648 to 2147483647.
static array bytesToSRational (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The rational found at offset will be returned as an array. There must be at least eight bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method bytesToSShort (line 261)

Extract a signed short from bytes.

  • return: the signed byte found at offset, e.g., an integer in the range -32768 to 32767.
static int bytesToSShort (string $bytes, int $offset, PelByteOrder $endian)
  • string $bytes: the bytes.
  • int $offset: the offset. The short found at offset will be returned as an integer. There must be at least two bytes available beginning at the offset given.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method longToBytes (line 133)

Convert an unsigned long into four bytes.

Because PHP limits the size of integers to 32 bit signed, one cannot really have an unsigned integer in PHP. But integers larger than 2^31-1 will be promoted to 64 bit signed floating point numbers, and so such large numbers can be handled too.

  • return: the bytes representing the unsigned long.
static string longToBytes (int $value, PelByteOrder $endian)
  • int $value: the unsigned long that will be converted. The argument will be treated as an unsigned 32 bit integer and the lower four bytes will be extracted. Treating the argument as an unsigned integer means that the absolute value will be used. Use sLongToBytes to convert signed integers.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method shortToBytes (line 88)

Convert an unsigned short into two bytes.

  • return: the bytes representing the unsigned short.
static string shortToBytes (int $value, PelByteOrder $endian)
  • int $value: the unsigned short that will be converted. The lower two bytes will be extracted regardless of the actual size passed.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method sLongToBytes (line 165)

Convert a signed long into four bytes.

  • return: the bytes representing the signed long.
static string sLongToBytes (int $value, PelByteOrder $endian)
  • int $value: the signed long that will be converted. The argument will be treated as a signed 32 bit integer, from which the lower four bytes will be extracted.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
static method sShortToBytes (line 107)

Convert a signed short into two bytes.

  • return: the bytes representing the signed short.
static string sShortToBytes (int $value, PelByteOrder $endian)
  • int $value: the signed short that will be converted. The lower two bytes will be extracted regardless of the actual size passed.
  • PelByteOrder $endian: one of LITTLE_ENDIAN and BIG_ENDIAN.
Class Constants
BIG_ENDIAN = false (line 74)

Big-endian (Motorola) byte order.

Data stored in big-endian byte order store the most significant byte first, so the number 0x12345678 becomes 0x12 0x34 0x56 0x78 when stored with big-endian byte order.

LITTLE_ENDIAN = true (line 65)

Little-endian (Intel) byte order.

Data stored in little-endian byte order store the least significant byte first, so the number 0x12345678 becomes 0x78 0x56 0x34 0x12 when stored with little-endian byte order.

Documentation generated on Thu, 05 May 2011 07:18:57 +0200 by phpDocumentor 1.4.3