Saturday, 15 August 2015

php - Is this algorithm reversible? -



php - Is this algorithm reversible? -

i have algorithm written in php project:

<?php $s = "abc"; //input -- string $n = strlen($s); $b = 0; ($i = 0; $i < $n; $i++) { $b += ord($s[$i]) * pow(31, ($n - ($i + 1))); } echo $b; //output -- int ?>

but have reverse take string integer. tried failed, there any way reverse it?

edit: "any way" meant doesn't have reverse original text, reverse text gives value.

if string can guaranteed have lowercase letters on it, can; you'll have figure out maths (i suggest work out in paper algorithm, leaving letters variables; solve equations , you'll see how reverse it).

if string arbitrary, no; because you're converting each character base-31 representation of number, shifting , adding results -- however, add-on has lots of carries, can't work out original characters digits (that is, final number) of result.

edit: given edit, yes, possible. can bit complicated, though -- i'll leave work out maths yourself. seek juggling bit number 31.

php algorithm

No comments:

Post a Comment