Ik heb volgende code en die werkt perfect.
Maar mijn probleem is dat de weergave gebeurd in een ID (<div id="gcCodeIdDiv"></div>)
Ik zou graag de omrekening hebben in een php variable $idcode.
Kan iemand hiermee helpen AUB ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GC Code Converter</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<div id="gcCodeIdDiv"></div>
<script>
<?php
$methode = "gcCode2Id";
$code = "GCA84W5";
?>
var method = <?php echo json_encode($methode); ?>;
var code = <?php echo json_encode($code); ?>;
function convertGCCode(method, chars) {
if (chars.length > 0) {
var newDiv = '<b>Result:</b><br>';
} else {
$('#gcCodeIdDiv').html('');
return;
}
var splitChars = chars.split(' ');
for (var i = 0; i < splitChars.length; i++) {
var input = splitChars[i];
if (method == 'gcCode2Id') {
input = input.substring(2);
var srcAlphabet = '0123456789ABCDEFGHJKMNPQRTVWXYZ';
var dstAlphabet = '0123456789';
} else {
var srcAlphabet = '0123456789';
if (parseInt(input, 10) + 411120 < 476656) {
var dstAlphabet = '0123456789ABCDEF';
} else {
var dstAlphabet = '0123456789ABCDEFGHJKMNPQRTVWXYZ';
input = (parseInt(input, 10) + 411120).toString();
}
}
var output = convertGC(input, srcAlphabet, dstAlphabet);
if (method == 'gcCode2Id') {
if (parseInt(output, 10) < 476656) {
var srcAlphabet = '0123456789ABCDEF';
output = convertGC(input, srcAlphabet, dstAlphabet);
} else {
output = (parseInt(output, 10) - 411120).toString();
}
if (output != 0) {
newDiv += output + ' ';
}
} else {
if (output != 0) {
newDiv += 'GC' + output + ' ';
}
}
}
$('#gcCodeIdDiv').html(newDiv);
}
function convertGC(src, srcAlphabet, dstAlphabet) {
var srcBase = srcAlphabet.length;
var dstBase = dstAlphabet.length;
var wet = src;
var val = 0;
var mlt = 1;
while (wet.length > 0) {
var digit = wet.charAt(wet.length - 1);
var digVal = srcAlphabet.indexOf(digit);
if (digVal > -1) {
val += mlt * digVal;
mlt *= srcBase;
}
wet = wet.substring(0, wet.length - 1);
}
wet = val;
var ret = "";
while (wet >= dstBase) {
var digitVal = wet % dstBase;
var digit = dstAlphabet.charAt(digitVal);
ret = digit + ret;
wet /= dstBase;
}
var digit = dstAlphabet.charAt(wet);
ret = digit + ret;
return ret;
}
convertGCCode(method, code);
</script>
</head>
</html>