Fix bug convertEncoding

This commit is contained in:
Divlo 2019-10-05 17:29:27 +02:00
parent 12f06d4dc3
commit cabf5239a3
2 changed files with 48 additions and 30 deletions

View File

@ -215,36 +215,54 @@ try {
} }
// Convertis des nombres de différents bases // Convertis des nombres de différents bases
function convertDecimalBinaryHexadecimal(value, option) { function convertDecimalBinaryHexadecimal(value, option) {
try { try {
if (option === 'DecimalToBinary') { if (option === 'DecimalToBinary') {
value = value.replace(" ", ""); value = value.replace(" ", "");
return parseInt(value).toString(2); value = parseInt(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(2);
}
} }
else if (option === 'BinaryToDecimal') { else if (option === 'BinaryToDecimal') {
return parseInt(value, 2); return parseInt(value, 2);
} }
else if (option === 'DecimalToHexadecimal') { else if (option === 'DecimalToHexadecimal') {
value = value.replace(" ", ""); value = value.replace(" ", "");
return parseInt(value).toString(16).toUpperCase(); value = parseInt(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(16).toUpperCase();
}
} }
else if (option === 'HexadecimalToDecimal') { else if (option === 'HexadecimalToDecimal') {
return parseInt(value, 16); return parseInt(value, 16);
} }
else if (option === 'BinaryToHexadecimal') { else if (option === 'BinaryToHexadecimal') {
value = parseInt(value, 2); value = parseInt(value, 2);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(16).toUpperCase(); return parseInt(value).toString(16).toUpperCase();
} }
}
else if (option === 'HexadecimalToBinary') { else if (option === 'HexadecimalToBinary') {
value = parseInt(value, 16); value = parseInt(value, 16);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(2); return parseInt(value).toString(2);
} }
}
else { else {
return messageError; return messageError;
} }
} }
catch (error) { catch (error) {
return messageError; return messageError;
} }
} }
// Convertis un nombre arabe en nombre romain // Convertis un nombre arabe en nombre romain

View File

@ -182,7 +182,7 @@ $(function () {
{ {
if (option === 'DecimalToBinary' || option === 'BinaryToDecimal' || option === 'DecimalToHexadecimal' || option === 'HexadecimalToDecimal' || option === 'BinaryToHexadecimal' || option === 'HexadecimalToBinary') { if (option === 'DecimalToBinary' || option === 'BinaryToDecimal' || option === 'DecimalToHexadecimal' || option === 'HexadecimalToDecimal' || option === 'BinaryToHexadecimal' || option === 'HexadecimalToBinary') {
let result = convertDecimalBinaryHexadecimal(value, option); let result = convertDecimalBinaryHexadecimal(value, option);
if (result === messageError || isNaN(result)) { if (result === messageError) {
$('.results').html(messageError); $('.results').html(messageError);
} else { } else {
$('.results').html(result); $('.results').html(result);