{"id":1067,"date":"2013-07-30T16:54:39","date_gmt":"2013-07-30T15:54:39","guid":{"rendered":"http:\/\/shotgundentist.com\/blog\/?page_id=1067"},"modified":"2013-10-02T16:23:01","modified_gmt":"2013-10-02T15:23:01","slug":"number-converter","status":"publish","type":"page","link":"https:\/\/shotgundentist.com\/blog\/usefull-tools\/number-converter\/","title":{"rendered":"Number converter"},"content":{"rendered":"<p><script type=\"text\/javascript\">\n  function update(obj) {\n    if (obj.id == 'base') obj = document.getElementById('xbase');\n    if (obj.id == 'alignHex') obj = document.getElementById('base10');\n    var inputs = document.getElementsByName(\"input\");\n    for (var i=0; i < inputs.length; i++) {\n      if (inputs[i].id != obj.id) {\n        inputs[i].value=addPadding(convertFromTo(obj.id, inputs[i].id, obj.value), inputs[i].id);\n      }\n    }\n  }\n  function convertFromTo(from, to, value) {\n    if (value == 0) return 0;\n    var fromBase = getBase(from);\n    var toBase = getBase(to);\n    return convert(value.replace(\/\\s+\/g, ''), fromBase, toBase);\n  }\n  function getBase(id) {\n    if (id == 'xbase') {\n      var element = document.getElementById('base');\n      var value = element.value;\n      return parseInt(value);\n    }\n    else if (id == 'alignHex') {\n      return 10;\n    }\n    return parseInt(id.substring(4));\n  }\n  function convert(value, from, to) {\n    return decimalToBaseX(baseXToDecimal(value, from), to);\n  }\n  function decimalToBaseX(value, base) {\n    if (base == 10) return value;\n    var result = '';\n    while (value > 0) {\n      var remainder = value % base;\n      result = (remainder < 10 ? remainder : String.fromCharCode('A'.charCodeAt(0) + (remainder-10))) + result;\n      value = Math.floor(value \/ base);\n    }\n    return result;\n  }\n  function baseXToDecimal(value, base) {\n    if (base == 10) return value;\n    var result = 0;\n    while(value.length > 0) {\n      result *= base;\n      var c = value.charCodeAt(0);\n      var digitValue = -1;\n      if (c >= 'a'.charCodeAt(0)) {\n        digitValue = 10 + (c - 'a'.charCodeAt(0));\n      } else if (c >= 'A'.charCodeAt(0)) {\n        digitValue = 10 + (c - 'A'.charCodeAt(0));\n      } else {\n        digitValue = c - '0'.charCodeAt(0);\n      }\n      if (digitValue >= 0) if (digitValue < base) result += digitValue;\n      value = value.substring(1);\n    }\n    return result;\n  }\n  var sevenSpaces = '       ';\n  function addPadding(value, base) {\n    padding = ' ';\n    var result = '';\n    var interval = value.length;\n    if (base == 'base2') interval = 8;\n    else if (base == 'base16') { interval = 2; if (document.getElementById('alignHex').checked) padding = sevenSpaces; }\n    else return value;\n    for (var i=0; i < value.length; i++) {\n      result += value.charAt(i);\n      if ((i+1) % interval == 0) {\n        result += padding;\n      }\n    }\n    return result;\n  }\n\/\/obj.value;\n<\/script><\/p>\n<p>Converts to and from any base encoding (max readable\/typeable is base36). Update any field to see the others immediately calculated. If there's a base you'd like added, let me know in a comment below and I'll add it.<\/p>\n<p><script type=\"text\/javascript\">\nfunction addInput(title, id) {\n  var table = document.getElementById('inputTable');\n  var row = table.insertRow(-1);\n  var cell1 = row.insertCell(0);\n  var cell2 = row.insertCell(1);\n  cell1.innerHTML='<label for=\"' + id + '\">' + title + ': <\/label>';\n  cell2.innerHTML='<input type=\"text\" name=\"input\" id=\"' + id + '\" oninput=\"update(this)\" size=\"60\" style=\"font-family:monospace;\"><\/input>';\n}  \nfunction addAllInputs() {\n  addInput('Decimal', 'base10');\n  addInput('Binary', 'base2');\n  addInput('Hex', 'base16');\n  addInput('Octal', 'base8');\n  addInput('Base32', 'base32');\n}\n<\/script><\/p>\n<form>\n<table id=\"inputTable\">\n<\/table>\n<\/form>\n<div style=\"font-weight:bold;text-align:center;\">Custom Base<\/div>\n<table>\n<tr>\n<td>\n    <label for=\"base\">Base: <\/label>\n  <\/td>\n<td>\n    <input type=\"text\" id=\"base\" value=\"10\" oninput=\"update(this)\" size=\"75\"><\/input>\n  <\/td>\n<\/tr>\n<tr>\n<td>\n    <label for=\"xbase\">Input: <\/label>\n  <\/td>\n<td>\n    <input type=\"text\" name=\"input\" id=\"xbase\" oninput=\"update(this)\" size=\"75\"><\/input>\n  <\/td>\n<\/tr>\n<\/table>\n<p><input type=\"checkbox\" name=\"alignHex\" id=\"alignHex\" onclick=\"update(this)\"><\/input><label for=\"alignHex\">Align hex with binary<\/label><\/p>\n<p><script type=\"text\/javascript\">\n  addAllInputs();\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converts to and from any base encoding (max readable\/typeable is base36). Update any field to see the others immediately calculated. If there&#8217;s a base you&#8217;d like added, let me know in a comment below and I&#8217;ll add it. Custom Base Base: Input: Align hex with binary<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":147,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1067","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/pages\/1067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/comments?post=1067"}],"version-history":[{"count":72,"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/pages\/1067\/revisions"}],"predecessor-version":[{"id":1172,"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/pages\/1067\/revisions\/1172"}],"up":[{"embeddable":true,"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/pages\/147"}],"wp:attachment":[{"href":"https:\/\/shotgundentist.com\/blog\/wp-json\/wp\/v2\/media?parent=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}