﻿// Angepasst für Zeichencode-Umrechnung http://www.5goldig.de
// ==========================================================================
// JavaScript-Umrechner für Dezimal-/Hexadezimalzahlen
// Copyright (C) 2006 Netzreport (netzreport.googlepages.com)
//
// Website: http://netzreport.googlepages.com/online_umrechner_fuer_dez_hexadez.html
//
// Hinweis: Ein einfacherer Umrechner, der die Funktionen parseInt() und
//          toString() nutzt, ist hier erhältlich:
//          http://netzreport.googlepages.com/online_umrechner_fuer_zahlensysteme.html
//
// Dieses Programm steht unter der GNU General Public License. Der
// rechtlich gültige Lizenzhinweis und Lizenztext sind leider nur
// in englischer Sprache verfügbar:
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//
// The GNU General Public License is also available from:
// http://www.gnu.org/copyleft/gpl.html
//
// Eine lokale Kopie der GNU General Public License ist hier abrufbar:
// http://netzreport.googlepages.com/gpl.txt
// ==========================================================================
//
// --------------------------------------------------------------------------
// 2006-11-17: First release
// --------------------------------------------------------------------------

// Global variables:
var decnumerals = "0123456789";
var hexnumerals = "0123456789ABCDEF";
// Higher values can lead to wrong results (due to JavaScript limitations?):
var maxdec = 4503599627370495;  // Math.pow(16,13)-1
var maxhex = 13;  // number of places

function dectohex () {
  // Clear output field:
  document.converter.hexadecimal.value = "";

  // Check if input is a legal decimal numeral:
  var decinput = document.converter.decimal.value
  for (var i = 0; i < decinput.length; i++) {
    if (decnumerals.indexOf(decinput.charAt(i)) == -1) {
      alert("Ungültige Dezimalzahl! Erlaubt sind nur die Ziffern: " + decnumerals);
      return;
    }
  }

  // Check if decimal numeral is not too large:
  if (decinput > maxdec) {
    alert("Bitte geben Sie eine Dezimalzahl nicht größer als " + maxdec + " ein!");
    return;
  }

  // Convert decimal to hexadecimal numeral:
  var decinput_temp = decinput;
  var factors = 1;
  while (decinput_temp > 15) {
    decinput_temp = Math.floor(decinput_temp / 16);
    factors++;
  }
  var hexoutput = "";
  var mathpow_temp = 0;
  for (var i = factors; i >= 1; i--) {
    mathpow_temp = Math.pow(16, i-1);
    hexoutput = hexoutput + hexnumerals.charAt(Math.floor(decinput / mathpow_temp));
    decinput = decinput - (Math.floor(decinput / mathpow_temp) * mathpow_temp);
  }
  document.converter.hexadecimal.value = hexoutput;
}

function hextodec () {
  // Clear output field:
  document.converter.decimal.value = "";

  // Check if input is a legal hexadecimal numeral:
  var hexinput = document.converter.hexadecimal.value.toUpperCase();
  for (var i = 0; i < hexinput.length; i++) {
    if (hexnumerals.indexOf(hexinput.charAt(i)) == -1) {
    document.converter.decimal.value = "Falsche Eingabe";
      return;
    }
  }

  // Check if hexadecimal numeral is not too large:
  if (hexinput.length > maxhex) {
    document.converter.decimal.value = "Falsche Eingabe";
    return;
  }

  // Convert hexadecimal to decimal:
  var decoutput = 0;
  for (var i = 0; i < hexinput.length; i++) {
    decoutput = decoutput + hexnumerals.indexOf(hexinput.charAt(i)) * Math.pow(16, hexinput.length - i-1);
  }
  document.converter.decimal.value = "Alt+0"+decoutput;
}