Sandra M.: Was muß ich am Script ändern damit die Ausgabe dreistellig ist

Beitrag lesen

Hallo,
hab ein HEX. to DEC. Editorscript was nicht so funktioniert wie ich es gern hätte.
Die Ausgabe sollte immer dreistellig sein.

Z.B.
Hex. :  FB 59 FB 01
Ausgabe als DEC. ist: 251 89 251 1
Sollte aber so aussehen: 251 089 251 001  immer dreistellig.

Was muß ich am Script ändern damit die Ausgabe dreistellig ist.

Freu mich auf eure Hilfe,
und bedanke mich.

Gruß
Sandra M.

----------------------
Hier das Script:
----------------------

<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><font size="2" color="#0000ff">Hex.&nbsp;</font></td>
<td>
<input id="hexinput" type="text" value="" style="width:36em; font-size:.9em" onkeydown="KeyClick(event,'hex');" />
<input id="hex2dec" type="button" title="Convert hexadecimal to decimal" style="font-size:.9em" value="Hex to Dec" accesskey="X"
onclick="document.getElementById('decinput').value=CalcDec(document.getElementById('hexinput').value);"/>
</td>
</tr>

<tr>
<td><font size="2" color="#0000ff">Dec.&nbsp;</font></td>
<td>
<input id="decinput" type="text" value="" style="width:36em; font-size:.9em" onkeydown="KeyClick(event,'dec');" />
<input id="dec2hex" type="button" title="Convert decimal to hexadecimal" style="font-size:.9em" value="Dec to Hex" accesskey="D"
onclick="document.getElementById('hexinput').value=CalcHex(document.getElementById('decinput').value);" />
</td>
</tr>
</table>

<script type="text/javascript" language="JavaScript1.2">
 var is_NN="Netscape"==navigator.appName;

if (document.all && !document.getElementById) {
  document.getElementById = function(id) {
   return document.all[id];
  }
 }
function CalcDec(hex) {
  // convert hex to dec

dec="";
  spaceorigin=0;
  spaceindex=0;
  len=hex.length;
  num="";

while (spaceorigin < len) {
    if (0>(spaceindex=hex.indexOf(" ",spaceorigin)))
      spaceindex=len;

num=hex.substring(spaceorigin,spaceindex);
    spaceorigin=1+spaceindex;

if ("" != num) {
      num=parseInt(num,16);
      if (true == isNaN(num))
           num="?";
      dec=dec+" "+num;
    }
  }

return dec.substring(1);
}
function CalcHex(dec,uppercase) {
  // convert hex to dec
  // not the most elegant or sophisticated solution, but it works

hex="";
  spaceorigin=0;
  spaceindex=0;
  len=dec.length;
  num="";

base="0123456789abcdef";
  if (true == uppercase)
    base=base.toUpperCase();

while (spaceorigin < len) {

hexnum="";
    neg="";

if (0>(spaceindex=dec.indexOf(" ",spaceorigin)))
      spaceindex=len;

num=dec.substring(spaceorigin,spaceindex);
    spaceorigin=1+spaceindex;

if ("" != num) {
      if (true == isNaN(num))
        hex=hex+" ?";
      else {

if ("-" == num.charAt(0)) {
           neg="-";
           num=num.substr(1);
        }

if (0<num) {
   while (0<(num=num/16)) {
            whole=Math.floor(num);
     remain=0;
     if (num != whole) {
              remain=16*(num-whole);
              num=whole;
            }

remain=base.charAt(remain);
     hexnum=remain+hexnum;
   }
        }
 else
    hexnum="0";

hex=hex+" "+neg+hexnum
      }
    }
  }
  return hex.substring(1);
}
function KeyClick(event,fld) {
  // capture Enter key to calculate conversion
  if (is_NN)
     var key = event.which;
  else
     var key = event.keyCode;

if (13 == key) {
    if ("hex" == fld)
      document.getElementById("decinput").value = CalcDec(document.getElementById("hexinput").value);
    else
      document.getElementById("hexinput").value = CalcHex(document.getElementById("decinput").value, document.getElementById("uppercase").checked);
    return 0;
  }
  else
    return 1;
}
</script>