skip to Main Content

$hexValue = "11503B58";

I want to get output:

Seed0 = 0x11
Seed1 = 0x50
Seed2 = 0x3B
Seed3 = 0x58

Seed0 ~ Seed3 should be also Hex value (not string).

2

Answers


  1. $hexValue = 0x11503B58;
    
    $result = array_map(
      'hexdec',
      array_filter(
        explode(
          "n",
          chunk_split(dechex($hexValue), 2)
        )
      )
    );
    
    print_r($result);
    

    Output:

    Array
    (
        [0] => 17   // decimal equivalent of 0x11
        [1] => 80   // decimal equivalent of 0x50
        [2] => 59   // etc.
        [3] => 88   // 
    )
    
    Login or Signup to reply.
  2. A simple quick and dirty way is converting it to a string and then split it.

    You can convert a hex value into hex representation string.

    var_dump(dechex(0x11503B58));
    
    string(8) "11503b58"
    

    The internal representation is always decimal.

    var_dump(0x11503B58)
    
    int(290470744)
    
    $hexValue = '11503B58';
    preg_match_all('/../', $hexValue, $matches);
    [$seed0, $seed1, $seed2, $seed3] = array_map('hexdec', $matches[0]);
    [$seeda, $seedb, $seedc, $seedd] = array_map(fn($v) => '0x' . strtoupper($v), $matches[0]);
    
    echo $seed0, ' : ', $seeda, PHP_EOL;
    echo $seed1, ' : ', $seedb, PHP_EOL;
    echo $seed2, ' : ', $seedc, PHP_EOL;
    echo $seed3, ' : ', $seedd, PHP_EOL;
    
    17 : 0x11
    80 : 0x50
    59 : 0x3B
    88 : 0x58
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search