JavaScripts -> Mathematik -> Rechnen in verschiedenen Zahlensystemen

Mit diesem Script kann man einfache arithmetische Berechnungen in einem oder verschiedenen Zahlensystemen wie z.B. dem Binär- oder Hexadezimalsystem durchführen. Dabei kann für jeden Operanden sowie für das Ergebnis die Basis von 2-36 separat eingestellt werden.

Kompatibilität getestet:
Beispiel Anzeigen >>

Script-Code

<script type="text/javascript">
<!--

var ziffern=new Array("0","1","2","3","4","5","6","7","8","9","A","B",
                      "C","D","E","F","G","H","I","J","K","L","M","N",
                      "O","P","Q","R","S","T","U","V","W","X","Y","Z");


function frombase(zahl,basis) {
  var i,val=0,pos=0,num=0;
  zahl=zahl.toUpperCase();
  for(i=zahl.length-1;i>=0;i--) {
    if(zahl.charCodeAt(i)>64) {
      val=zahl.charCodeAt(i)-55;
    }
    else val=zahl.charCodeAt(i)-48;
    if(val>=basis || val<0) {
      return -1;
    }
    num+=val*Math.pow(parseInt(basis),pos);
    pos++;
  }
  return num;
}

function tobase(zahl,basis) {
  var r,rval="";
  do {
    r=zahl % basis;
    zahl=(zahl-r)/basis;
    rval=ziffern[r]+rval;
  } while(zahl>0);
  return rval;
}

function calculate() {
  var op0,op1,res;
  if(isNaN(parseInt(document.calc.basis0.value)) || parseInt(document.calc.basis0.value)<2
     || parseInt(document.calc.basis0.value)>36 || isNaN(parseInt(document.calc.basis1.value))
     || parseInt(document.calc.basis1.value)<2 || parseInt(document.calc.basis1.value)>36
     || isNaN(parseInt(document.calc.basisr.value)) || parseInt(document.calc.basisr.value)<2
     || parseInt(document.calc.basisr.value)>36 ) {
    document.calc.result.value="";
    document.calc.status.value="Ungültige Basis";
    return;
  }

  op0=frombase(document.calc.operand0.value,parseInt(document.calc.basis0.value));
  op1=frombase(document.calc.operand1.value,parseInt(document.calc.basis1.value));
  if((op0<0) || (op1<0)) {
    document.calc.result.value="";
    document.calc.status.value="Ungültige Zahl";
    return;
  }
  document.calc.status.value=op0+" "+document.calc.operation.value+" "+op1;
  switch(document.calc.operation.value) {
    case "+": res=op0+op1; break;
    case "-": res=op0-op1; break;
    case "*": res=op0*op1; break;
    case "/": res=(op0-op0%op1)/op1; break;
    case "%": res=op0%op1; break;
  }
  document.calc.status.value+=" = "+res;

  document.calc.result.value=tobase(res,parseInt(document.calc.basisr.value));
}


//-->
</script>

<!------------------------------------------------------->

<form name="calc">
<table>
<tr>
<td colspan="8" style="text-align:left">
Bitte geben sie sinnvolle Werte f&uuml;r alle gelb hinterlegten Fekder an. Die Ziffern sind 0...9, sowie A...Z. Erlaubte Basen sind 2 bis 36.

</td>
</tr>
<tr>
<th>Erster Operand</th><th>zur Basis</th><th>Operation</th><th>Zweiter Operand</th><th>zur Basis</th><th>&nbsp;</th><th>(Ergebnis)</th><th>zur Basis</th>
</tr>
<tr>
<td>
<input type="Text" name="operand0" value="0">
</td>
<td>

<input type="Text" name="basis0" value="10" size="5" maxlength="2">
</td>
<td>
<select name="operation" size="1">
<option value="+">+
<option value="*">*
<option value="-">-
<option value="/">div
<option value="%">mod
</select>
</td>
<td>
<input type="Text" name="operand1" value="0">
</td>
<td>
<input type="Text" name="basis1" value="10" size="5" maxlength="2">
</td>

<td>
<button type="button" onClick="calculate()"> = </button>
</td>
<td>
<input type="Text" name="result" style="background-color:#e0e0e0">
</td>
<td>
<input type="Text" name="basisr" value="10" size="5" maxlength="2">
</td>
</tr>
<tr>
<th colspan="8">
Zur Information: Rechnung im Dezimalsystem (Basis 10)
</th>
</tr>

<tr>
<td colspan="8">
<input type="Text" name="status" value="" style="background-color:#e0e0e0">
</td>
</tr>
</table>
</form>