questa è la conversione anche con javascript, magari può servire a qualcuno:Originariamente inviato da filippo.toso
Dovrebbe essere una cosa di questo tipo:
Non avendo la descrizione dell'algoritmo ne dei valori di test, non posso garantire l'affidabilità.Codice PHP:
<?php
function check_digit($code, $append = true) {
if (strlen($code) !== 11) {
return false;
}
$total = 0;
for ($i = 0; $i < 11; $i++) {
if ($i % 2 == 0) {
$total += (int)$code[$i];
} else {
$total += (int)$code[$i] * 11;
}
}
$string = (string)$total;
$total = 0;
$count = strlen($string);
for($i = 0; $i < $count; $i++) {
$total += (int)$string[$i];
}
$result = $total % 10;
return $append ? $code . $result : $result;
}
print check_digit('13586854035');
?>
Edit: errata corrige, piccolo errore
Edit 2: l'ho provato con due codici di raccomandate e sembra funzionare correttamente.
function check_digit(code, append) {
if (code.length !== 11) {
return false;
}
total = 0;
for (var i = 0; i < 11; i++) {
if (i % 2 == 0) {
total+=parseInt(code.substring(i, i+1));
} else {
total+=parseInt(code.substring(i, i+1)) * 11;
}
}
stringa_total = String(total);
total = 0;
count = stringa_total.length;
for(var i = 0; i < count; i++) {
total += parseInt(parseInt(stringa_total.substring(i, i+1)));
}
result = total % 10;
return append ? parseInt(code.result) : parseInt(result);
}