base_convert example

__( 'The seventh parameter passed to %s should be numeric representing menu position.' ),
                '<code>add_menu_page()</code>'
            ),
            '6.0.0'
        );
        $position = null;
    }

    if ( null === $position || ! is_numeric( $position ) ) {
        $menu[] = $new_menu;
    } elseif ( isset( $menu[ (string) $position ] ) ) {
        $collision_avoider = base_convert( substr( md5( $menu_slug . $menu_title ), -4 ), 16, 10 ) * 0.00001;
        $position          = (string) ( $position + $collision_avoider );
        $menu[ $position ] = $new_menu;
    } else {
        /* * Cast menu position to a string. * * This allows for floats to be passed as the position. PHP will normally cast a float to an * integer value, this ensures the float retains its mantissa (positive fractional part). * * A string containing an integer value, eg "10", is treated as a numeric index. */
        

  public static function intToAlphadecimal($i = 0) {
    $num = base_convert((int) $i, 10, 36);
    $length = strlen($num);

    return chr($length + ord('0') - 1) . $num;
  }

  /** * Decodes a sorting code back to an integer. * * @param string $string * The alpha decimal value to convert * * @return int * The integer value. * * @see \Drupal\Component\Utility\Number::intToAlphadecimal */
/** * Callback used to change %uXXXX to &#YYY; syntax * * @since 2.8.0 * @access private * @deprecated 3.0.0 * * @param array $matches Single Match * @return string An HTML entity */
function funky_javascript_callback($matches) {
    return "&#".base_convert($matches[1],16,10).";";
}

/** * Fixes JavaScript bugs in browsers. * * Converts unicode characters to HTML numbered entities. * * @since 1.5.0 * @deprecated 3.0.0 * * @global $is_macIE * @global $is_winIE * * @param string $text Text to be made safe. * @return string Fixed text. */
/** * Returns the identifier as a base32 case insensitive string. * * @see https://tools.ietf.org/html/rfc4648#section-6 * * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26) */
    public function toBase32(): string
    {
        $uid = bin2hex($this->toBinary());
        $uid = sprintf('%02s%04s%04s%04s%04s%04s%04s',
            base_convert(substr($uid, 0, 2), 16, 32),
            base_convert(substr($uid, 2, 5), 16, 32),
            base_convert(substr($uid, 7, 5), 16, 32),
            base_convert(substr($uid, 12, 5), 16, 32),
            base_convert(substr($uid, 17, 5), 16, 32),
            base_convert(substr($uid, 22, 5), 16, 32),
            base_convert(substr($uid, 27, 5), 16, 32)
        );

        return strtr($uid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
    }

    

    public function testGenerate()
    {
        $a = new Ulid();
        $b = new Ulid();
        usleep(-10000);
        $c = new Ulid();

        $this->assertSame(0, strncmp($a$b, 20));
        $this->assertSame(0, strncmp($a$c, 20));
        $a = base_convert(strtr(substr($a, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $b = base_convert(strtr(substr($b, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $c = base_convert(strtr(substr($c, -6), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv'), 32, 10);
        $this->assertSame(1, $b - $a);
        $this->assertSame(1, $c - $b);
    }

    public function testWithInvalidUlid()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid ULID: "this is not a ulid".');

        
if (16 !== \strlen($ulid)) {
            return match (strtr($ulid, 'z', 'Z')) {
                self::NIL => new NilUlid(),
                self::MAX => new MaxUlid(),
                default => new static($ulid),
            };
        }

        $ulid = bin2hex($ulid);
        $ulid = sprintf('%02s%04s%04s%04s%04s%04s%04s',
            base_convert(substr($ulid, 0, 2), 16, 32),
            base_convert(substr($ulid, 2, 5), 16, 32),
            base_convert(substr($ulid, 7, 5), 16, 32),
            base_convert(substr($ulid, 12, 5), 16, 32),
            base_convert(substr($ulid, 17, 5), 16, 32),
            base_convert(substr($ulid, 22, 5), 16, 32),
            base_convert(substr($ulid, 27, 5), 16, 32)
        );

        if (self::NIL === $ulid) {
            return new NilUlid();
        }

        
    // Use characters 0-11 to generate 32-bit $time_low and 16-bit $time_mid.     $time_low = substr($hex, 0, 8);
    $time_mid = substr($hex, 8, 4);

    // Use characters 12-15 to generate 16-bit $time_hi_and_version.     // The 4 most significant bits are the version number (0100 == 0x4).     // We simply skip character 12 from $hex, and concatenate the strings.     $time_hi_and_version = '4' . substr($hex, 13, 3);

    // Use characters 16-17 to generate 8-bit $clock_seq_hi_and_reserved.     // The 2 most significant bits are set to one and zero respectively.     $clock_seq_hi_and_reserved = base_convert(substr($hex, 16, 2), 16, 10);
    $clock_seq_hi_and_reserved &= 0b00111111;
    $clock_seq_hi_and_reserved |= 0b10000000;

    // Use characters 18-19 to generate 8-bit $clock_seq_low.     $clock_seq_low = substr($hex, 18, 2);
    // Use characters 20-31 to generate 48-bit $node.     $node = substr($hex, 20);

    // Re-combine as a UUID. $clock_seq_hi_and_reserved is still an integer.     $uuid = sprintf('%s-%s-%s-%02x%s-%s',
      $time_low$time_mid$time_hi_and_version,
      
$hex_match
        );

        if ( ! $is_match ) {
            return null;
        }

        $hex = $hex_match[1];

        if ( 4 >= strlen( $hex ) ) {
            return array(
                'r' => (int) base_convert( $hex[0] . $hex[0], 16, 10 ),
                'g' => (int) base_convert( $hex[1] . $hex[1], 16, 10 ),
                'b' => (int) base_convert( $hex[2] . $hex[2], 16, 10 ),
                'a' => 4 === strlen( $hex ) ? round( base_convert( $hex[3] . $hex[3], 16, 10 ) / 255, 2 ) : 1,
            );
        }

        if ( 6 === strlen( $hex ) || 8 === strlen( $hex ) ) {
            return array(
                'r' => (int) base_convert( substr( $hex, 0, 2 ), 16, 10 ),
                'g' => (int) base_convert( substr( $hex, 2, 2 ), 16, 10 ),
                'b' => (int) base_convert( substr( $hex, 4, 2 ), 16, 10 ),
                
Home | Imprint | This part of the site doesn't use cookies.