New function : convertBinaryText

This commit is contained in:
Divlo
2019-09-14 17:38:30 +02:00
parent a422b9fa67
commit 22c1524dcf
7 changed files with 95 additions and 4 deletions

View File

@ -100,7 +100,7 @@ function calculateAge(birthDateEntered) {
}
}
// Convertit des °C en °F et l'inverse aussi
// Convertis des °C en °F et l'inverse aussi
function convertTemperature(degree, unit) {
if (!isNaN(degree) && unit === "°C")
{
@ -118,7 +118,7 @@ function convertTemperature(degree, unit) {
}
}
// Convertit la longueur (distance) avec les unités allant de picomètre au Téramètre
// Convertis la longueur (distance) avec les unités allant de picomètre au Téramètre
function convertDistance (firstValue, unitFirstValue, unitFinalValue) {
let reference = ["pm",null,null,"nm",null,null,"µm",null,null,"mm","cm","dm","m","dam","hm","km",null,null,"Mm",null,null,"Gm",null,null,"Tm"];
@ -224,6 +224,27 @@ 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;
}
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);
}
return decodeURIComponent(out);
}
/////////////////////////////////////////////////////////////////
/* Fonctions Annexes */
@ -407,4 +428,9 @@ function timeZone(json) {
}
let timeZoneStr = timeZoneValue.toString();
return dateTimeUTC(timeZoneStr);
}
// Verifie si une chaîne de caractère c'est du binaire ou non
function isStringBin(s) {
return s.match(/^[+\-0-1(). ]+$/g) !== null ? true : false;
}

View File

@ -179,6 +179,32 @@ $(function () {
}
});
$("#submitConvertBinaryText").click(function()
{
let binaryTextValue = $('#binaryTextValue').val();
let isBinaryValue = isStringBin(binaryTextValue);
if(isEmptyValue(binaryTextValue)) {
$('.results').html(emptyMessageError);
}
else if (isBinaryValue === true) {
// Le replace enlève les espaces
let textResult = binToUtf8(binaryTextValue.replace(/\s/g,''));
$('.results').html(textResult);
}
else if (isBinaryValue === false) {
// Les 2 replace permettent de rajouter un espace tout les 8 bits
let binaryResult = utf8ToBin(binaryTextValue);
binaryResult = binaryResult.replace(/(\d{8})/g, '$1 ').replace(/(^\s+|\s+$)/,'');
$('.results').html(binaryResult);
}
else {
$('.results').html(messageError);
}
});
/* Permet d'afficher l'heure en temps réel sur le footer */
window.onload = realDateTime('realDateTime');