//注意点是转化为二进制后字符串不够4位,故用('0000'+str).substr(-4)来填充
let oddListIndex = [];
let evenListIndex = [];
let sortFn = (a,b)=>{
return a.charCodeAt()-b.charCodeAt()
}
let oddList = str
.filter((item, index) => {
if (index % 2 !== 0) {
oddListIndex.push(index);
}
return index % 2 !== 0;
})
.sort(sortFn);
let evenList = str
.filter((item, index) => {
if (index % 2 == 0) {
evenListIndex.push(index);
}
return index % 2 == 0;
})
.sort(sortFn);
while (oddList.length) {
let index = oddListIndex.shift();
let st = oddList.shift();
str[index] = st;
}
while (evenList.length) {
let index = evenListIndex.shift();
let st = evenList.shift();
str[index] = st;
}
let newStr = str.map((item, index, arr) => {
let judge1 = item.charCodeAt() >= 65 && item.charCodeAt() <= 70;
let judge2 = item.charCodeAt() >= 97 && item.charCodeAt() <= 102;
let judge3 = item.charCodeAt() >= 48 && item.charCodeAt() <= 57;
if (judge1 || judge2 || judge3) {
let dec = parseInt(item, 16);
let bin = parseInt(('0000'+dec.toString(2)).substr(-4).split("").reverse().join(""), 2)
.toString(16)
.toUpperCase();
return bin;
}else{
return item
}
});
console.log(newStr.join(""));