From 59001fa41e17a63a0b8d6a2cb0473114e2d907a3 Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 4 Nov 2019 16:36:04 +0100 Subject: [PATCH] Hotfix convertEncoding --- scripts/fonctions_principales.js | 12 +++++++----- scripts/main.js | 8 +------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/scripts/fonctions_principales.js b/scripts/fonctions_principales.js index cea40f7..781c6c0 100644 --- a/scripts/fonctions_principales.js +++ b/scripts/fonctions_principales.js @@ -262,13 +262,13 @@ function numberUnicodeToText(string) { function textToBinary(s) { try { s = unescape( encodeURIComponent(s)); - var chr, i = 0, l = s.length, out = ''; + let chr, i = 0, l = s.length, out = ''; for( ; i < l; i ++ ){ chr = s.charCodeAt( i ).toString(2); while(chr.length % 8 != 0 ){ chr = '0' + chr; } out += chr; } - return out; + return out.replace(/(\d{8})/g, '$1 ').replace(/(^\s+|\s+$)/,''); } catch (error) { return s; } @@ -276,7 +276,8 @@ function textToBinary(s) { // Binaire (UTF-8) en Texte function binaryToText(s){ try { - var i = 0, l = s.length, chr, out = ''; + s = s.replace(/\s/g,'') + let i = 0, l = s.length, chr, out = ''; for( ; i < l; i += 8){ chr = parseInt( s.substr(i, 8 ), 2).toString(16); out += '%' + ((chr.length % 2 == 0) ? chr : '0' + chr); @@ -290,13 +291,13 @@ function binaryToText(s){ function textToHexadecimal (s) { try { s = unescape( encodeURIComponent( s ) ); - var chr, i = 0, l = s.length, out = ''; + let chr, i = 0, l = s.length, out = ''; for( ; i < l; i++ ){ chr = s.charCodeAt( i ).toString( 16 ); out += ( chr.length % 2 == 0 ) ? chr : '0' + chr; out += " "; } - return out; + return out.toUpperCase(); } catch (error) { return s; @@ -305,6 +306,7 @@ function textToHexadecimal (s) { // Hexadécimal (UTF-8) en Texte function hexadecimalToText (s) { try { + s = s.replace(/\s/g,''); return decodeURIComponent( s.replace( /../g, '%$&' ) ); } catch (error) { diff --git a/scripts/main.js b/scripts/main.js index 8684468..3233af8 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -131,17 +131,11 @@ $(function () { const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText }; try { function executionFunction(option, value) { - if (convertEncoding[option]) { - return convertEncoding[option](value) - } else { - console.log(convertEncoding[option]); - return messageError; - } + return convertEncoding[option](value) } $('.results').html(executionFunction(option, value)); } catch (error) { $('.results').html(messageError); - console.log(error); } } });