From c455a1e66b175670d3192c894c60f7f6fa4ae9fc Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 11 Aug 2020 22:30:19 +0800 Subject: [PATCH] finish converter --- SuperScriptViewer/static/converter.js | 108 +++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 12 deletions(-) diff --git a/SuperScriptViewer/static/converter.js b/SuperScriptViewer/static/converter.js index c7f6ce0..7c0b6f8 100644 --- a/SuperScriptViewer/static/converter.js +++ b/SuperScriptViewer/static/converter.js @@ -14,16 +14,16 @@ function doConv1_I2O() { for (var forwardIndex = 0; forwardIndex < lineSp.length; forwardIndex++) { var words = lineSp[forwardIndex]; words = words.trim().toLowerCase(); - if (!checkByteUnit(words)){ + if (!checkByteUnit(words)) { resultCache.length = 0; successful = false; break; - } else + } else resultCache[innerIndex] = words; - innerIndex--; + innerIndex--; } - if (successful) + if (successful) result[index] = resultCache.join(", "); else result[index] = ""; @@ -49,16 +49,16 @@ function doConv2_I2O() { for (var forwardIndex = 0; forwardIndex < lineSp.length; forwardIndex++) { var words = lineSp[forwardIndex]; words = words.trim().toLowerCase(); - if (!checkByteUnit(words)){ + if (!checkByteUnit(words)) { resultCache.length = 0; successful = false; break; - } else + } else resultCache = words.substr(2, 2) + resultCache; - innerIndex--; + innerIndex--; } - if (successful) + if (successful) result[index] = resultCache; else result[index] = ""; @@ -69,7 +69,38 @@ function doConv2_I2O() { } function doConv2_O2I() { - + var textSp = $("#conv2Output").val().split("\n"); + var result = new Array(); + + for (var index = 0; index < textSp.length; index++) { + var lines = textSp[index].toLowerCase(); + var resultCache = new Array(); + + if (!checkHexInput(lines)) { + result[index] = ""; + continue; + } + + var sectorCount = parseInt(lines.length / 2); + var remainCount = lines.length % 2; + + for (var innerIndex = 0; innerIndex < sectorCount; innerIndex++) { + if (innerIndex == 0) + resultCache[innerIndex] = "0x" + lines.slice(-(innerIndex + 1) * 2); + else + resultCache[innerIndex] = "0x" + lines.slice(-(innerIndex + 1) * 2, -innerIndex * 2); + } + if (remainCount != 0) + resultCache[sectorCount] = "0x0" + lines[0]; + + for (var addedIndex = resultCache.length; addedIndex < 4; addedIndex++) { + resultCache[addedIndex] = "0x00"; + } + + result[index] = resultCache.join(", "); + } + + $("#conv2Input").val(result.join("\n")); } // ============================= conv 3 @@ -88,13 +119,13 @@ function doConv3_I2O() { for (var forwardIndex = 0; forwardIndex < lineSp.length; forwardIndex++) { var words = lineSp[forwardIndex]; words = words.trim().toLowerCase(); - if (!checkByteUnit(words)){ + if (!checkByteUnit(words)) { resultCache.length = 0; successful = false; break; - } else + } else resultCache = words.substr(2, 2) + resultCache; - innerIndex--; + innerIndex--; } if (successful) { @@ -110,7 +141,41 @@ function doConv3_I2O() { } function doConv3_O2I() { + var textSp = $("#conv3Output").val().split("\n"); + var result = new Array(); + for (var index = 0; index < textSp.length; index++) { + var declines = textSp[index]; + var resultCache = new Array(); + + if (!checkDecInput(declines)) { + result[index] = ""; + continue; + } + + var bigInt = BigInt(declines); + var lines = bigInt.toString(16).toLowerCase(); + + var sectorCount = parseInt(lines.length / 2); + var remainCount = lines.length % 2; + + for (var innerIndex = 0; innerIndex < sectorCount; innerIndex++) { + if (innerIndex == 0) + resultCache[innerIndex] = "0x" + lines.slice(-(innerIndex + 1) * 2); + else + resultCache[innerIndex] = "0x" + lines.slice(-(innerIndex + 1) * 2, -innerIndex * 2); + } + if (remainCount != 0) + resultCache[sectorCount] = "0x0" + lines[0]; + + for (var addedIndex = resultCache.length; addedIndex < 4; addedIndex++) { + resultCache[addedIndex] = "0x00"; + } + + result[index] = resultCache.join(", "); + } + + $("#conv3Input").val(result.join("\n")); } // ============================= conv 4 @@ -125,6 +190,25 @@ function doConv4_O2I() { // ============================= utils +function checkDecInput(s) { + var legalWords = "0123456789"; + if (s.length == 0) return false; + for (var i = 0; i < s.length; i++) { + if (legalWords.indexOf(s[i]) == -1) + return false; + } + return true; +} + +function checkHexInput(s) { + var legalWords = "0123456789abcdef"; + for (var i = 0; i < s.length; i++) { + if (legalWords.indexOf(s[i]) == -1) + return false; + } + return true; +} + function checkByteUnit(s) { var legalWords = "0123456789abcdefABCDEF"; if (s.length != 4) return false;