js hex conversion binary binary to hex exclusive or algorithm

Why write a separate method to do the conversion between different bases? Because when the self-contained method is converted from hexadecimal to binary, the accuracy will be lost after 13 characters. In order to keep the accuracy from being lost, I wrote two methods according to the principle of converting hexadecimal to binary

If the length does not exceed 13 bits, use parseInt ("125adf8", 16). ToString (2) ='10010110101111111000 ',

Hexadecimal conversion binary (the conversion method does not consider the case with decimal point)

function hex_to_bin(str) {
            let hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},
                {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}]

            let value=""
            for(let i=0;i<str.length;i++){
                for(let j=0;j<hex_array.length;j++){
                    if(str.charAt(i)== hex_array[j].key){
                        value = value.concat(hex_array[j].val)
                        break
                    }
                }
            }
            console.log(value)
            return value
        }

Binary to hexadecimal (this conversion method does not consider the case with decimal point)

function bin_to_hex(str) {
            let hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"},
                {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}]
            let value = ''
            let list=[]
            console.log(str)
            if(str.length%4!==0){
                let a = "0000"
                let b=a.substring(0,4-str.length%4)
                str = b.concat(str)
            }
            console.log(str)
            while (str.length > 4) {
                list.push(str.substring(0, 4))
                str = str.substring(4);
            }
            list.push(str)
            console.log(list)
            for(let i=0;i<list.length;i++){
                for(let j=0;j<hex_array.length;j++){
                    if(list[i]==hex_array[j].val){
                        value = value.concat(hex_array[j].key)
                        break
                    }
                }
            }
            console.log(value)
            return value
        }

Exclusive or operation (based on the above two methods)

function xor(a ,b){
            let A = hex_to_bin(a)
            let B = hex_to_bin(b)
            console.log(a+"   a Binary system:"+A)
            console.log(b+"   b Binary system:"+B)
            let o = "00000000000000000000000000000000000"
            if(A.toString().length > B.toString().length){
                let c = A.toString().length - B.toString().length
                B = o.substr(0,c).concat(B)
            }else if(A.toString().length < B.toString().length){
                let c = B.toString().length - A.toString().length
                A = o.substr(0,c).concat(A)
            }
            console.log('B:'+B)
            console.log('A:'+A)
            let d = ""
            for(let i=0;i<A.toString().length;i++){
                if(A.toString()[i]==B.toString()[i]){
                    let q="0"
                    d = d.concat(q)
                }else{
                    let p="1"
                    d = d.concat(p)
                }
            }
            console.log(bin_to_hex(d))
            return bin_to_hex(d)
        }

Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/lixiwoaini/article/details/82179094

Posted by azukah on Thu, 02 Jan 2020 10:56:10 -0800