skip to Main Content

I have a 16-character, input string composed of hexadecimal characters. I need to isolate the last two characters, extend that string to four characters by repeating the two isolated characters, then replace them using an associative lookup/map.

My sample input is:

$atcode = '11F5796B61690196';

My associative array is:

$sub1 = [
    '0' => '2',
    '1' => '3',
    '2' => '0',
    '3' => '1',
    '4' => '6',
    '5' => '7',
    '7' => '5',
    '8' => 'A',
    '9' => 'B',
    'A' => '8',
    'B' => '9',
    'C' => 'E',
    'D' => 'F',
    'E' => 'C',
    'F' => 'D'
];

The isolated characterd should be 9 and 6.

The desired result is: B6B6

This is the code so far:

$codearray = str_split($atcode);
    
//select the 15th digit from $codearray and name it $one
$one = $codearray[14];
    
//create an associative array and name it $sub1
$sub1 = array('0' => '2', '1' => '3', '3' => '1', 'D' => 'F', '4' => '6', '5' => '7', '7' => '5', '8' => 'A', '9' => 'B', 'A' => '8', 'B' => '9', 'C' => 'E', '2' => '0', 'E' => 'C', 'F' => 'D');
    
//search through $sub1, determine which value is equivalent to $one, and select its equivalency from the key to value pairs
$codeone = array_replace($one, $sub1);
    
//select the 16th digit from $codearray and name it $two
$two = $codearray[15];
    
echo "$codeone", "$two"; 

However, I am just getting a blank page.

3

Answers


  1. Once you have initialised $atcode, I suggest you could simplify your code to this:

    $sub1 = array('0' => '2', '1' => '3', '3' => '1', 'D' => 'F', '4' => '6', '5' => '7', '7' => '5', '8' => 'A', '9' => 'B', 'A' => '8', 'B' => '9', 'C' => 'E', '2' => '0', 'E' => 'C', 'F' => 'D');
    echo str_repeat($sub1[$atcode[14]] . $atcode[15], 2);
    
    Login or Signup to reply.
  2. Change $codeone = array_replace($one, $sub1); to $codeone = $sub1[$one];

    $codearray = str_split("11F5796B61690196");
        
        
        $one = $codearray[14];
    
    // Create an associative array and name it $sub1
    $sub1 = array('0' => '2', '1' => '3', '3' => '1', 'D' => 'F', '4' => '6', '5' => '7', '7' => '5', '8' => 'A', '9' => 'B', 'A' => '8', 'B' => '9', 'C' => 'E', '2' => '0', 'E' => 'C', 'F' => 'D');
    
    // Search through $sub1, determine which value is equivalent to $one, and select its equivalency from the key to value pairs
    $codeone = $sub1[$one];
    
    // Select the 16th digit from $codearray and name it $two
    $two = $codearray[15];
    
    // Output the 4-digit code
    echo $codeone . $two.$codeone . $two;
    

    This should give B6B6

    Login or Signup to reply.
  3. Abandon most of that code.

    Extract the last two characters, repeat them, then translate them with strtr().

    Code: (Demo)

    $atcode = '11F5796B61690196';
    
    var_export(
        strtr(
            str_repeat(substr($atcode, -2), 2),
            '012345789ABCDEF',
            '2301675AB89EFCD'
        )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search