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
Once you have initialised
$atcode
, I suggest you could simplify your code to this:Change $codeone = array_replace($one, $sub1); to $codeone = $sub1[$one];
This should give B6B6
Abandon most of that code.
Extract the last two characters, repeat them, then translate them with
strtr()
.Code: (Demo)