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) {
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) {

View File

@ -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;
}
}
$('.results').html(executionFunction(option, value));
} catch (error) {
$('.results').html(messageError);
console.log(error);
}
}
});