Hotfix convertEncoding

This commit is contained in:
Divlo 2019-11-04 16:36:04 +01:00
parent 150e8f5116
commit 59001fa41e
2 changed files with 8 additions and 12 deletions

View File

@ -262,13 +262,13 @@ function numberUnicodeToText(string) {
function textToBinary(s) { function textToBinary(s) {
try { try {
s = unescape( encodeURIComponent(s)); s = unescape( encodeURIComponent(s));
var chr, i = 0, l = s.length, out = ''; let chr, i = 0, l = s.length, out = '';
for( ; i < l; i ++ ){ for( ; i < l; i ++ ){
chr = s.charCodeAt( i ).toString(2); chr = s.charCodeAt( i ).toString(2);
while(chr.length % 8 != 0 ){ chr = '0' + chr; } while(chr.length % 8 != 0 ){ chr = '0' + chr; }
out += chr; out += chr;
} }
return out; return out.replace(/(\d{8})/g, '$1 ').replace(/(^\s+|\s+$)/,'');
} catch (error) { } catch (error) {
return s; return s;
} }
@ -276,7 +276,8 @@ function textToBinary(s) {
// Binaire (UTF-8) en Texte // Binaire (UTF-8) en Texte
function binaryToText(s){ function binaryToText(s){
try { 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){ for( ; i < l; i += 8){
chr = parseInt( s.substr(i, 8 ), 2).toString(16); chr = parseInt( s.substr(i, 8 ), 2).toString(16);
out += '%' + ((chr.length % 2 == 0) ? chr : '0' + chr); out += '%' + ((chr.length % 2 == 0) ? chr : '0' + chr);
@ -290,13 +291,13 @@ function binaryToText(s){
function textToHexadecimal (s) { function textToHexadecimal (s) {
try { try {
s = unescape( encodeURIComponent( s ) ); s = unescape( encodeURIComponent( s ) );
var chr, i = 0, l = s.length, out = ''; let chr, i = 0, l = s.length, out = '';
for( ; i < l; i++ ){ for( ; i < l; i++ ){
chr = s.charCodeAt( i ).toString( 16 ); chr = s.charCodeAt( i ).toString( 16 );
out += ( chr.length % 2 == 0 ) ? chr : '0' + chr; out += ( chr.length % 2 == 0 ) ? chr : '0' + chr;
out += " "; out += " ";
} }
return out; return out.toUpperCase();
} }
catch (error) { catch (error) {
return s; return s;
@ -305,6 +306,7 @@ function textToHexadecimal (s) {
// Hexadécimal (UTF-8) en Texte // Hexadécimal (UTF-8) en Texte
function hexadecimalToText (s) { function hexadecimalToText (s) {
try { try {
s = s.replace(/\s/g,'');
return decodeURIComponent( s.replace( /../g, '%$&' ) ); return decodeURIComponent( s.replace( /../g, '%$&' ) );
} }
catch (error) { catch (error) {

View File

@ -131,17 +131,11 @@ $(function () {
const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText }; const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText };
try { try {
function executionFunction(option, value) { function executionFunction(option, value) {
if (convertEncoding[option]) {
return convertEncoding[option](value) return convertEncoding[option](value)
} else {
console.log(convertEncoding[option]);
return messageError;
}
} }
$('.results').html(executionFunction(option, value)); $('.results').html(executionFunction(option, value));
} catch (error) { } catch (error) {
$('.results').html(messageError); $('.results').html(messageError);
console.log(error);
} }
} }
}); });