I am trying to translate the following C code, which basically just tries to convert an arbitrary integer value into a character from a pool of characters, into PHP:
#include <cstdint>
#include <cstring>
#include <iostream>
uint8_t GetCharacter(uint32_t value) {
static const char* valid_characters = "0123456789ABCDEFGHIJKLMOPQRSTUVWabcdefghijklmnopqrstuvw";
static const size_t valid_characters_l = strlen(valid_characters);
uint8_t c = valid_characters[value % valid_characters_l];
return valid_characters[(value << c) % valid_characters_l];
}
int main() {
uint32_t array[] = {176, 52, 608, 855};
for (size_t i=0; i < 4; i++) {
uint8_t c = GetCharacter(array[i]);
std::cout << array[i] << ": " << (uint32_t) c << "\n";
}
return 0;
}
Which yields
176: 109
52: 114
608: 85
855: 65
The PHP code I've been able to come up with however yields the following:
176: 109
52: 114
608: 85
855: 104 // << Here's the problem
I am very sure I translated it exactly and I am unable to find the issue.
<?php
function getCharacter($index) {
$chars = "0123456789ABCDEFGHIJKLMOPQRSTUVWabcdefghijklmnopqrstuvw";
$c = ord(substr($chars, $index % strlen($chars)));
return ord(substr($chars, ($index << $c) % strlen($chars)));
}
function main() {
$array = array(176, 52, 608, 855);
foreach ($array as $value) {
echo "$value: " . getCharacter($value) . "\n";
}
}
main();
Could someone point me into the right direction to solve this problem?