*/ public static function to_byte_array( $text ) { $text = (string) $text; if ( '' === $text ) { return array(); } $bytes = unpack( 'C*', $text ); return is_array( $bytes ) ? array_values( $bytes ) : array(); } /** * Run length encode a module string into alternating bar and space widths. * * The module string is expected to start with a bar, so the first returned * width is a bar width. * * @param string $bits Module string made of "0" and "1". * @return array */ public static function rle( $bits ) { $elements = array(); $previous = null; $run = 0; $length = strlen( $bits ); for ( $i = 0; $i < $length; $i++ ) { $bit = $bits[ $i ]; if ( $bit === $previous ) { ++$run; continue; } if ( null !== $previous ) { $elements[] = $run; } $previous = $bit; $run = 1; } if ( null !== $previous ) { $elements[] = $run; } return $elements; } }