当我正在寻找十六进制值时,我得到二进制作为答案

时间:2017-04-04 15:17:27

标签: javascript html

好的我正在尝试这个简单的代码,它采用ascii并将其转换为hex dec oct html和binary。但每次我尝试循环十六进制十进制或html它给我转换为二进制而不是相应的。顺便说一下,我不允许使用charcodeat



//declare global variables
var outputConvDEC, outputConvHEX, outputConvOCT, outputConvHTML, outputConvBINARY;
//This will get ascii input and convert it and show output.
function convertASCII() {
  //declare local variables
  var userInput, valueASCII, splitChar, i;
  //define variables
  userInput = document.getElementById("charInput").value;
  outputConvHEX = '';
  splitChar = new Array();
  splitChar = userInput.split('');

  for (i = 0; i < splitChar.length; i++) {
    outputConvDEC = outputConvDEC + getConversion(splitChar[i]);
  }

  showASCII(valueASCII);

}
//This will check the input with corresponding switchcase and get the value of each variable.
function getConversion(ofInputChar) {
  //declare local variables
  var alertError, preHex, preHtml;
  //define local variables
  preHex = "0x"
  preHtml = "&#"
  alertError = window.alert;
  //check for the characterinput in the switchcase and return the value.
  switch (ofInputChar) {
    case " ":
      outputConvDEC = " ";
      outputConvHEX = " ";
      outputConvOCT = " ";
      outputConvHTML = " ";
      outputConvBINARY = " ";
      break;
    case "a":
      outputConvDEC = "97";
      outputConvHEX = preHex + "61";
      outputConvOCT = "141";
      outputConvHTML = preHtml + "97";
      outputConvBINARY = "01100001";
      break;
    case "b":
      outputConvDEC = "98";
      outputConvHEX = preHex + "62";
      outputConvOCT = "142";
      outputConvHTML = preHtml + "98";
      outputConvBINARY = "01100010";
      break;
    case "c":
      outputConvDEC = "99";
      outputConvHEX = preHex + "63";
      outputConvOCT = "143";
      outputConvHTML = preHtml + "99";
      outputConvBINARY = "01100011";
      break;
    case "d":
      outputConvDEC = "100";
      outputConvHEX = preHex + "64";
      outputConvOCT = "144";
      outputConvHTML = preHtml + "100";
      outputConvBINARY = "01100100";
      break;
    case "e":
      outputConvDEC = "101";
      outputConvHEX = preHex + "65";
      outputConvOCT = "145";
      outputConvHTML = preHtml + "101";
      outputConvBINARY = "01100101";
      break;
    case "f":
      outputConvDEC = "102";
      outputConvHEX = preHex + "66";
      outputConvOCT = "146";
      outputConvHTML = preHtml + "102";
      outputConvBINARY = "01100110";
      break;
    case "g":
      outputConvDEC = "103";
      outputConvHEX = preHex + "67";
      outputConvOCT = "147";
      outputConvHTML = preHtml + "103";
      outputConvBINARY = "01100111";
      break;
    case "h":
      outputConvDEC = "104";
      outputConvHEX = preHex + "68";
      outputConvOCT = "150";
      outputConvHTML = preHtml + "104";
      outputConvBINARY = "01101000";
      break;
    case "i":
      outputConvDEC = "105";
      outputConvHEX = preHex + "69";
      outputConvOCT = "151";
      outputConvHTML = preHtml + "105";
      outputConvBINARY = "01101001";
      break;
    case "j":
      outputConvDEC = "106";
      outputConvHEX = preHex + "6A";
      outputConvOCT = "152";
      outputConvHTML = preHtml + "106";
      outputConvBINARY = "01101010";
      break;
    case "k":
      outputConvDEC = "107";
      outputConvHEX = preHex + "6B";
      outputConvOCT = "153";
      outputConvHTML = preHtml + "107";
      outputConvBINARY = "01101011";
      break;
    case "l":
      outputConvDEC = "108";
      outputConvHEX = preHex + "6C";
      outputConvOCT = "154";
      outputConvHTML = preHtml + "108";
      outputConvBINARY = "01101100";
      break;
    case "m":
      outputConvDEC = "109";
      outputConvHEX = preHex + "6D";
      outputConvOCT = "155";
      outputConvHTML = preHtml + "109";
      outputConvBINARY = "01101101";
      break;
    case "n":
      outputConvDEC = "110";
      outputConvHEX = preHex + "6E";
      outputConvOCT = "156";
      outputConvHTML = preHtml + "110";
      outputConvBINARY = "01101110";
      break;
    case "o":
      outputConvDEC = "111";
      outputConvHEX = preHex + "6F";
      outputConvOCT = "157";
      outputConvHTML = preHtml + "111";
      outputConvBINARY = "01101111";
      break;
    case "p":
      outputConvDEC = "112";
      outputConvHEX = preHex + "70";
      outputConvOCT = "160";
      outputConvHTML = preHtml + "112";
      outputConvBINARY = "01110000";
      break;
    case "q":
      outputConvDEC = "113";
      outputConvHEX = preHex + "71";
      outputConvOCT = "161";
      outputConvHTML = preHtml + "113";
      outputConvBINARY = "01110001";
      break;
    case "r":
      outputConvDEC = "114";
      outputConvHEX = preHex + "72";
      outputConvOCT = "162";
      outputConvHTML = preHtml + "114";
      outputConvBINARY = "01110010";
      break;
    case "s":
      outputConvDEC = "115";
      outputConvHEX = preHex + "73";
      outputConvOCT = "163";
      outputConvHTML = preHtml + "115";
      outputConvBINARY = "01110011";
      break;
    case "t":
      outputConvDEC = "116";
      outputConvHEX = preHex + "74";
      outputConvOCT = "164";
      outputConvHTML = preHtml + "116";
      outputConvBINARY = "01110100";
      break;
    case "u":
      outputConvDEC = "117";
      outputConvHEX = preHex + "75";
      outputConvOCT = "165";
      outputConvHTML = preHtml + "117";
      outputConvBINARY = "01110101";
      break;
    case "v":
      outputConvDEC = "118";
      outputConvHEX = preHex + "76";
      outputConvOCT = "166";
      outputConvHTML = preHtml + "118";
      outputConvBINARY = "01110110";
      break;
    case "w":
      outputConvDEC = "119";
      outputConvHEX = preHex + "77";
      outputConvOCT = "167";
      outputConvHTML = preHtml + "119";
      outputConvBINARY = "01110111";
      break;
    case "x":
      outputConvDEC = "120";
      outputConvHEX = preHex + "78";
      outputConvOCT = "170";
      outputConvHTML = preHtml + "120";
      outputConvBINARY = "01111000";
      break;
    case "y":
      outputConvDEC = "121";
      outputConvHEX = preHex + "79";
      outputConvOCT = "171";
      outputConvHTML = preHtml + "121";
      outputConvBINARY = "01111001";
      break;
    case "z":
      outputConvDEC = "122";
      outputConvHEX = preHex + "7A";
      outputConvOCT = "172";
      outputConvHTML = preHtml + "122";
      outputConvBINARY = "01111010";
      break;
      //Default case value.
    default:
      //if charInput.value has a symbol it will send out a error and replace everywhere there was symbol with hastags
      document.getElementById("charInput").value = "";
      outputConvDEC = "##";
      outputConvHEX = "##";
      outputConvOCT = "##";
      outputConvHTML = "##";
      outputConvBINARY = "##";
  }
  //return the output.value of charInput.value to corresponding variable.
  return outputConvDEC, outputConvHEX, outputConvOCT, outputConvHTML, outputConvBINARY;

}

function showASCII(showText) {
  document.getElementById("asciiConvDEC").value = outputConvDEC;
  document.getElementById("asciiConvHEX").value = outputConvHEX;
  document.getElementById("asciiConvOCT").value = outputConvOCT;
  document.getElementById("asciiConvHTML").value = outputConvHTML;
  document.getElementById("asciiConvBINARY").value = outputConvBINARY;
}
&#13;
<head>
  <!-- Title -->
  <title>ASCII Conversion Tool</title>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>

<body style="text-align:center">
  <!-- Header Text -->
  <font face="verdana">
    <h1 style="font-size:200%">Programming Project #2</h1>
    <h2 style="font-size:140%">ASCII Conversion Tool</h2>
    <p>
      ASCII<br />
      <textarea autofocus id="charInput" rows="4" cols="60"></textarea> <br /> to
    </p>
    <!-- Execute conversion of ASCII -->
    <form action="">
      <input type="checkbox" id="convInputSpace"> Convert Space Between Text
    </form><br />
    <button onclick="convertASCII()">Convert</button>
    <p>
      <!-- Output for Conversion. Textbox to show the output, Output of text is readonly -->
      DECIMAL<br />
      <textarea id="asciiConvDEC" rows="4" cols="100" readonly></textarea><br /> HEX
      <br />
      <textarea id="asciiConvHEX" rows="4" cols="100" readonly></textarea><br /> OCTAL
      <br />
      <textarea id="asciiConvOCT" rows="4" cols="100" readonly></textarea><br /> HTML
      <br />
      <textarea id="asciiConvHTML" rows="4" cols="100" readonly></textarea><br /> BINARY
      <br />
      <textarea id="asciiConvBINARY" rows="4" cols="100" readonly></textarea><br />
    </p>
  </font>

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是实现结果的极其复杂的方法。 这实际上只是几行:

https://jsfiddle.net/1wfb9p4u/

function convertASCII() {

  var userInput = document.getElementById("charInput").value;
  var outputConvDEC = '';
  var outputConvHEX = '';
  var outputConvOCT = '';
  var outputConvBIN = '';
  var outputConvHTML = '';

  var splitChar = userInput.split('');

  for (i = 0; i < splitChar.length; i++) {
    var charCode = splitChar[i].charCodeAt(0);
    outputConvDEC += charCode + ' ';
    outputConvHEX += '0x' + charCode.toString(16) + ' ';
    outputConvOCT += '0' + charCode.toString(8) + ' ';
    outputConvBIN += '0b' + charCode.toString(2) + ' ';
    outputConvHTML += '&#' + charCode + ';';
  }

  document.getElementById("asciiConvDEC").value = outputConvDEC;
  document.getElementById("asciiConvHEX").value = outputConvHEX;
  document.getElementById("asciiConvOCT").value = outputConvOCT;
  document.getElementById("asciiConvHTML").value = outputConvHTML;
  document.getElementById("asciiConvBINARY").value = outputConvBIN;
}