Horde_Ldap_Util::asc2hex32 PHP Method

asc2hex32() public static method

Converts all ASCII chars < 32 to "\HEX".
public static asc2hex32 ( string $string ) : string
$string string String to convert.
return string Hexadecimal representation of $string.
    public static function asc2hex32($string)
    {
        for ($i = 0, $len = strlen($string); $i < $len; $i++) {
            $char = substr($string, $i, 1);
            if (ord($char) < 32) {
                $hex = dechex(ord($char));
                if (strlen($hex) == 1) {
                    $hex = '0' . $hex;
                }
                $string = str_replace($char, '\\' . $hex, $string);
            }
        }
        return $string;
    }

Usage Example

Example #1
0
 /**
  * Test asc2hex32()
  */
 public function testAsc2hex32()
 {
     $expected = '\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0a\\0b\\0c\\0d\\0e\\0f\\10\\11\\12\\13\\14\\15\\16\\17\\18\\19\\1a\\1b\\1c\\1d\\1e\\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
     $str = '';
     for ($i = 0; $i < 127; $i++) {
         $str .= chr($i);
     }
     $this->assertEquals($expected, Horde_Ldap_Util::asc2hex32($str));
 }