From 8d11d1b8045fcbb69af88ab52680bf3a5e961f8b Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 15 Sep 2019 22:21:02 +0200 Subject: [PATCH] try ... catch error for convertBinaryText --- scripts/fonctions.js | 32 ++++++++++++++++++++------------ scripts/main.js | 1 - 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/scripts/fonctions.js b/scripts/fonctions.js index 34685e5..bdddd20 100644 --- a/scripts/fonctions.js +++ b/scripts/fonctions.js @@ -227,23 +227,31 @@ function convertCurrency(currency, euroValue) { // Convertis du texte (encodé en UTF-8) en binaire et l'inverse aussi (source : http://jsfiddle.net/47zwb41o) // UTF-8 vers Binaire function utf8ToBin(s) { - s = unescape( encodeURIComponent(s)); - var 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; + try { + s = unescape( encodeURIComponent(s)); + var 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; + } catch (error) { + return s; } - return out; } // Binaire vers UTF-8 function binToUtf8(s){ - var 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); + try { + var 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); + } + return decodeURIComponent(out); + } catch (error) { + return s; } - return decodeURIComponent(out); } ///////////////////////////////////////////////////////////////// diff --git a/scripts/main.js b/scripts/main.js index 8f27b4d..254c416 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -185,7 +185,6 @@ $(function () { { let binaryTextValue = $('#binaryTextValue').val(); let convertIn = $("#convertIn option:selected").text(); - let isBinaryValue = isStringBin(binaryTextValue); if(isEmptyValue(binaryTextValue)) { $('.results').html(emptyMessageError);