convertEncoding Update

This commit is contained in:
Divlo
2019-10-24 18:57:39 +02:00
parent b5f4cfc900
commit c4a76a398f
5 changed files with 139 additions and 113 deletions

View File

@ -172,8 +172,94 @@ function convertCurrency(value, currency, url) {
}
// Convertis des nombres de différentes bases et convertis en UTF-8. (source : http://jsfiddle.net/47zwb41o)
// DecimalToBinary
function decimalToBinary(value) {
value = value.replace(" ", "");
value = Number(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(2);
}
}
// BinaryToDecimal
function binaryToDecimal(value) {
value = Number(value);
const result = formatNumberResult(parseInt(value, 2));
if (isNaN(result)) {
return messageError;
} else {
return result
}
}
// DecimalToHexadecimal
function decimalToHexadecimal(value) {
value = value.replace(" ", "");
value = Number(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(16).toUpperCase();
}
}
// HexadecimalToDecimal
function hexadecimalToDecimal(value) {
const result = formatNumberResult(parseInt(value, 16));
if (isNaN(result)) {
return messageError;
} else {
return result;
}
}
// BinaryToHexadecimal
function binaryToHexadecimal(value) {
value = Number(value);
value = parseInt(value, 2);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(16).toUpperCase();
}
}
// HexadecimalToBinary
function hexadecimalToBinary(value) {
value = Number(value);
value = parseInt(value, 16);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(2);
}
}
// Each letters has its own codePoint (Unicode Code)
function textToNumberUnicode(string) {
try {
let resultat = "";
for (let index in string) {
resultat = resultat + string.codePointAt(index) + " ";
}
return resultat;
}
catch(error) {
return messageError;
}
}
// Each codePoint has its own letter
function numberUnicodeToText(string) {
try {
const array = string.split(" ");
let resultat = "";
for (let index in array) {
resultat = resultat + String.fromCodePoint(parseInt(array[index]).toString());
}
return resultat;
}
catch(error) {
return messageError;
}
}
// Texte en Binaire (UTF-8)
function utf8ToBin(s) {
function textToBinary(s) {
try {
s = unescape( encodeURIComponent(s));
var chr, i = 0, l = s.length, out = '';
@ -188,7 +274,7 @@ function utf8ToBin(s) {
}
}
// Binaire (UTF-8) en Texte
function binToUtf8(s){
function binaryToText(s){
try {
var i = 0, l = s.length, chr, out = '';
for( ; i < l; i += 8){
@ -201,7 +287,7 @@ function binToUtf8(s){
}
}
// Texte en Hexadécimal (UTF-8)
function utf8ToHex (s) {
function textToHexadecimal (s) {
try {
s = unescape( encodeURIComponent( s ) );
var chr, i = 0, l = s.length, out = '';
@ -217,7 +303,7 @@ function utf8ToHex (s) {
}
}
// Hexadécimal (UTF-8) en Texte
function hexToUtf8 (s) {
function hexadecimalToText (s) {
try {
return decodeURIComponent( s.replace( /../g, '%$&' ) );
}
@ -225,52 +311,6 @@ function hexToUtf8 (s) {
return s;
}
}
// Convertis des nombres de différents bases
function convertDecimalBinaryHexadecimal(value, option) {
try {
switch (option) {
case 'DecimalToBinary':
value = value.replace(" ", "");
value = parseInt(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(2);
}
case 'BinaryToDecimal':
return formatNumberResult(parseInt(value, 2));
case 'DecimalToHexadecimal':
value = value.replace(" ", "");
value = parseInt(value);
if (isNaN(value)) {
return messageError;
} else {
return value.toString(16).toUpperCase();
}
case 'HexadecimalToDecimal':
return formatNumberResult(parseInt(value, 16));
case 'BinaryToHexadecimal':
value = parseInt(value, 2);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(16).toUpperCase();
}
case 'HexadecimalToBinary':
value = parseInt(value, 16);
if (isNaN(value)) {
return messageError;
} else {
return parseInt(value).toString(2);
}
default:
return messageError;
}
}
catch (error) {
return messageError;
}
}
// Convertis un nombre arabe en nombre romain
function convertArabicToRoman(nombre) {

View File

@ -2,7 +2,7 @@ $(function () {
/* ÉXECUTION DES FONCTONS */
// Touche entrer génère un clique
// Touche entrer génère un clique sur les classes .btn
$("body").keydown(function(e){
if(e.which === 13){
$(".btn").click();
@ -39,8 +39,7 @@ $(function () {
$("#birthDateValue").bind("keyup change", () =>
{
const birthDateEntered = $('#birthDateValue').val();
$('.results').html(calculateAge(birthDateEntered));
$('.results').html(calculateAge($('#birthDateValue').val()));
});
$("#submitConvertTemperature").click(() =>
@ -104,7 +103,7 @@ $(function () {
for (index in quotes) {
resultat = resultat + `<tr> <td class="quote-element-list important">${quotes[index]["source"]}</td> <td class="quote-element-list">${quotes[index]["quote"]}</td> </tr>`;
}
$( ".quote-list" ).append(resultat);
$(".quote-list").append(resultat);
}
$("#submitConvertCurrency").click(() => {
@ -128,32 +127,22 @@ $(function () {
$('.results').html(messageError);
}
else {
if (option === 'DecimalToBinary' || option === 'BinaryToDecimal' || option === 'DecimalToHexadecimal' || option === 'HexadecimalToDecimal' || option === 'BinaryToHexadecimal' || option === 'HexadecimalToBinary') {
const result = convertDecimalBinaryHexadecimal(value, option);
$('.results').html(result);
}
else if (option === 'BinaryToText') {
// Le replace enlève les espaces
const textResult = binToUtf8(value.replace(/\s/g,''));
$('.results').html(textResult);
}
else if (option === 'TextToBinary') {
// Les 2 replace permettent de rajouter un espace tout les 8 bits
let binaryResult = utf8ToBin(value);
binaryResult = binaryResult.replace(/(\d{8})/g, '$1 ').replace(/(^\s+|\s+$)/,'');
$('.results').html(binaryResult);
}
else if (option === 'TextToHexadecimal') {
const result = utf8ToHex(value);
$('.results').html(result.toUpperCase());
}
else if (option === 'HexadecimalToText') {
const result = hexToUtf8(value.replace(/\s/g,''));
$('.results').html(result);
}
else {
$('.results').html(messageError);
}
// Objet qui recense toutes les fonctions de convertEncoding
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);
}
}
});
@ -216,6 +205,21 @@ $(function () {
$('.results').html(`Il y a ${formatNumberResult(stringPermutationsResult.length)} possibilités d'anagramme pour le mot "${value}" qui contient ${value.length} caractères, la liste : <br><br> ${result}`);
}
});
/* Changement du texte accueil (exemples de fonction) */
if(chemin === "/" || chemin === '/index.php') {
let index=-1;
function change() {
if(index === texteFonctionChange.length-1) {
index = 0;
}
else {
index++;
}
document.getElementById("change").innerHTML = texteFonctionChange[index];
}
setInterval(change,10000);
}
/* Permet d'afficher l'heure en temps réel sur le footer */
window.onload = realDateTime('realDateTime');
@ -244,21 +248,6 @@ $(function () {
language: 'fr',
autoclose: false,
todayHighlight: true
})
});
})
/* Changement du texte accueil (exemples de fonction) */
if(chemin === "/" || chemin === '/index.php') {
let index=-1;
function change() {
if(index === texteFonctionChange.length-1) {
index = 0;
}
else {
index++;
}
document.getElementById("change").innerHTML = texteFonctionChange[index];
}
setInterval("change()",10000);
}
});