Hexadecimal string script
Javascript program listing
/* ------------------------------------------------------------------------------ */
function tilde(val) {
var swap_tab = [0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0];
val = parseInt(val,16);
var lib = val & 0x0f;
var mib = (val & 0xf0) >> 4;
var tmp = swap_tab[mib] << 4;
tmp |= swap_tab[lib];
return tmp < 16 ? '0' + tmp.toString(16) : tmp.toString(16);
}
/* ------------------------------------------------------------------------------ */
function count_one(val) {
var count_tab = [0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];
val = parseInt(val,16);
var lib = val & 0x0f;
var mib = (val & 0xf0) >> 4;
return count_tab[lib] + count_tab[mib];
}
/* ------------------------------------------------------------------------------ */
function bit_swap(val) {
var swap_tab = [0x0,0x8,0x4,0xc,0x2,0xa,0x6,0xe,0x1,0x9,0x5,0xd,0x3,0xb,0x7,0xf];
val = parseInt(val,16);
var lib = val & 0x0f;
var mib = (val & 0xf0) >> 4;
var tmp = swap_tab[lib] << 4;
tmp |= swap_tab[mib];
return tmp < 16 ? '0' + tmp.toString(16) : tmp.toString(16);
}
/* ------------------------------------------------------------------------------ */
function display_result(str, ok) {
if (ok) {
var count=0, tmp="";
var ls = str.length;
for (var j=0; j < ls; j+=2) {
count += count_one(str.substring(j, j+2));
tmp += str.substring(j, j+2);
tmp += " ";
}
document.hexa_out.result.value = tmp;
document.hexa_out.nb_bytes.value = ls/2;
document.hexa_out.nb_zero.value = ls*4 - count;
document.hexa_out.nb_one.value = count;
} else {
document.hexa_out.result.value = str;
document.hexa_out.nb_bytes.value = "";
document.hexa_out.nb_zero.value = "";
document.hexa_out.nb_one.value = "";
}
}
/* ------------------------------------------------------------------------------ */
function mirror_bytes(str) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
for (var i=0; i < ls; i+=2) {
out += bit_swap(str.substring(i, i+2));
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function mirror_entry(str) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
for (var i=ls; i>0; i-=2) {
out += bit_swap(str.substring(i-2, i));
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function comp_one(str) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
for (var i=0; i < ls; i+=2) {
out += tilde(str.substring(i, i+2));
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function shift_left(str, shift) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
shift = parseInt(shift);
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
if ((shift < ls*4) && (shift>0)) {
var so = Math.floor(shift/4);
var sb = shift % 4;
var lsa = ls, stra = str;
for (var j=0; j<=so; j++) {stra += "0"; lsa += 1;}
while (shift+4 < lsa*4) {
var tmp = (parseInt(stra.substring(so, so+2), 16) >> (4-sb)) & 0x0f;
out += tmp.toString(16);
so ++; shift += 4;
}
} else {
out = "Shift available range = [1 .. " + ((ls*4)-1).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function rotate_left(str, shift) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
shift = parseInt(shift);
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
if ((shift < ls*4) && (shift>0)) {
var so = Math.floor(shift/4);
var sb = shift % 4;
var lsa = ls, stra = str;
for (var j=0; j <= so; j++) {stra += stra.charAt(j); lsa += 1;}
while (shift+4 < lsa*4) {
var tmp = (parseInt(stra.substring(so, so+2), 16) >> (4-sb)) & 0x0f;
out += tmp.toString(16);
so ++; shift += 4;
}
} else {
out = "Rotation available range = [1 .. " + ((ls*4)-1).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function shift_right(str, shift) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
shift = parseInt(shift);
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
if ((shift < ls*4) && (shift>0)) {
var so = Math.floor(shift/4);
var sb = shift % 4;
var lsa = ls, stra = str;
for (var j=0; j <= so; j++) {stra = "0" + stra; lsa += 1;}
//console.log("so = " + so + " sb = " + sb + " shift = " + shift + " lsa = " + lsa + " str = " + str);
var loop = 0;
while (shift+4 < lsa*4) {
var tmp = (parseInt(stra.substring(loop, loop+2), 16) >> sb) & 0x0f;
out += tmp.toString(16);
//console.log("tmp = " + tmp + " so = " + so + " str = " + str.substring(so, so+2));
loop ++; shift += 4;
}
} else {
out = "Shift available range = [1 .. " + ((ls*4)-1).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function rotate_right(str, shift) {
str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "";
if (isOk) {
shift = parseInt(shift);
var ls = str.length;
if (ls&1) {str = "0" + str; ls += 1; document.hexa_in.str1.value = str;}
if ((shift < ls*4) && (shift>0)) {
var so = Math.floor(shift/4); var sb = shift % 4;
var lsa = ls, stra = str;
for (var j=0; j <= so; j++) {stra = stra.charAt(ls-j-1) + stra; lsa += 1;}
var loop = 0;
while (shift+4 < lsa*4) {
var tmp = (parseInt(stra.substring(loop, loop+2), 16) >> sb) & 0x0f;
out += tmp.toString(16);
loop ++; shift += 4;
}
} else {
out = "Rotation available range = [1 .. " + ((ls*4)-1).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function remove_bits(str, from, nbit) {
var str = str.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str);
var out = "", tmp, tmp1;
if (isOk) {
from = parseInt(from); nbit = parseInt(nbit);
var ls = str.length;
if (((from+nbit) <= ls*4) && (nbit>0)) {
var so = Math.floor(from/4), sb = from % 4;
var no = Math.floor((from+nbit)/4), nb = (from+nbit) % 4;
for (var j=0; j < so; j++) {out += str.charAt(j);}
tmp1 = parseInt(str.charAt(so), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp = tmp1.substring(0, sb);
tmp1 = parseInt(str.charAt(no), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp += tmp1.substring(nb,4);
for (var j=no+1; j < ls; j++) {
tmp1 = parseInt(str.charAt(j), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp += tmp1;
}
var lo = tmp.length; var lb = lo%4;
while (lb < 4) {tmp += '0'; lb++}
for (var j=0; j < lo; j+=4) {
out += parseInt(tmp.substring(j, j+4), 2).toString(16);
}
} else {
out = "Remove number of bits range = [1 .. " + ((ls*4)-1).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function add_bits(str1, str2, from, nbit) {
str1 = str1.replace(/\s/g, ""); str2 = str2.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str1); isOk &= /^[0-9A-F]+$/i.test(str2);
var out = "", tmp, tmp1, tmp2;
if (isOk) {
from = parseInt(from); nbit = parseInt(nbit);
var ls1 = str1.length; var ls2 = str2.length;
if (from<=ls1*4) {
if ((nbit>0) && (nbit<=ls2*4)) {
var so = Math.floor(from/4); var sb = from % 4;
var no = Math.floor(nbit/4); var nb = nbit % 4;
for (var j=0; j < so; j++) {out += str1.charAt(j);}
tmp1 = parseInt(str1.charAt(so), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp = tmp1.substring(0, sb);
for (var j=0; j < no; j++) {
tmp2 = parseInt(str2.charAt(j), 16).toString(2);
while (tmp2.length < 4) tmp2 = '0' + tmp2;
tmp += tmp2;
}
tmp2 = parseInt(str2.charAt(no), 16).toString(2);
while (tmp2.length < 4) tmp2 = '0' + tmp2;
tmp += tmp2.substring(0, nb);
if (so < ls1) {
tmp += tmp1.substring(sb, 4);
}
for (var j=so+1; j < ls1; j++) {
tmp1 = parseInt(str1.charAt(j), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp += tmp1;
}
var lo = tmp.length; var lb = lo%4;
while (lb < 4) {tmp += '0'; lb++}
for (var j=0; j < lo; j+=4) {
out += parseInt(tmp.substring(j, j+4), 2).toString(16);
}
} else {
out = "Add available bit range = [1 .. " + (ls2*4).toString() + "]";
isOk = false;
}
} else {
out = "Insert from bit range = [0 .. " + (ls1*4).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function replace_bits(str1, str2, from, nbit) {
str1 = str1.replace(/\s/g, ""); str2 = str2.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str1); isOk &= /^[0-9A-F]+$/i.test(str2);
var out="", tmp, tmp1, tmp2;
if (isOk) {
from = parseInt(from); nbit = parseInt(nbit);
var ls1 = str1.length; var ls2 = str2.length;
if ((from0) && (nbit<=ls2*4)) {
var so = Math.floor(from/4); var sb = from % 4;
var no = Math.floor(nbit/4); var nb = nbit % 4;
for (var j=0; j < so; j++) {out += str1.charAt(j);}
tmp1 = parseInt(str1.charAt(so), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp = tmp1.substring(0, sb);
for (var j=0; j < no; j++) {
tmp2 = parseInt(str2.charAt(j), 16).toString(2);
while (tmp2.length < 4) tmp2 = '0' + tmp2;
tmp += tmp2;
}
tmp2 = parseInt(str2.charAt(no), 16).toString(2);
while (tmp2.length < 4) tmp2 = '0' + tmp2;
tmp += tmp2.substring(0, nb);
no = Math.floor((from+nbit)/4), nb = (from+nbit) % 4;
if (no < ls1) {
tmp1 = parseInt(str1.charAt(no), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp += tmp1.substring(nb,4);
}
for (var j=no+1; j < ls1; j++) {
tmp1 = parseInt(str1.charAt(j), 16).toString(2);
while (tmp1.length < 4) tmp1 = '0' + tmp1;
tmp += tmp1;
}
var lo = tmp.length;
for (var j=0; j < lo; j+=4) {
out += parseInt(tmp.substring(j, j+4), 2).toString(16);
}
} else {
out = "Replace available range = [1 .. " + (ls2*4).toString() + "]";
isOk = false;
}
} else {
out = "Replace bit range = [0 .. " + (ls1*4).toString() + "]";
isOk = false;
}
} else {
out = "Input empty or not detected as an hexadecimal string";
}
display_result(out, isOk);
}
/* ------------------------------------------------------------------------------ */
function str_1_2(str1, str2, op) {
str1 = str1.replace(/\s/g, ""); str2 = str2.replace(/\s/g, "");
var isOk = /^[0-9A-F]+$/i.test(str1); isOk &= /^[0-9A-F]+$/i.test(str2);
var out = "", j;
if (isOk) {
var ls1 = str1.length, ls2 = str2.length;
if (ls1 & 1) {str1 = "0" + str1; ls1 += 1;}
if (ls2 & 1) {str2 = "0" + str2; ls2 += 1;}
if (ls1 > ls2) {
var ls = ls1-ls2;
for (j=0; j < ls; j++) {str2 = "0" + str2; ls2 += 1;}
} else if (ls2 > ls1) {
var ls = ls2-ls1;
for (j=0; j < ls; j++) {str1 = "0" + str1; ls1 += 1;}
}
document.hexa_in.str1.value = str1;
document.hexa_in.str2.value = str2;
for (j=0; j < ls1; j++) {
var val1 = parseInt(str1.charAt(j), 16);
var val2 = parseInt(str2.charAt(j), 16);
switch (op) {
case 0:
out += (val1&val2).toString(16);
break;
case 1:
out += (val1|val2).toString(16);
break;
case 2:
out += (val1^val2).toString(16);
break;
}
}
} else {
out = "Input data not detected as an hexadecimal string";
}
display_result(out, isOk);
}