javascript uint8数组和uint32之间的转换

低位在前,高位在后

function intTobytes(value){
var a=new Uint8Array(4)
a[3]=(value >> 24) & 0xFF

        a[2]=(value >> 16) & 0xFF

        a[1]=(value >> 8) & 0xFF

        a[0]=value  & 0xFF

        return a;
  }

function bytesToint(value){

    var a=new Uint32Array(1)

        a[0]= (value[3] & 0xFF)<<24 

        a[0]=a[0] | ((value[2] & 0xFF) <<16)

        a[0]=a[0] | ((value[1] & 0xFF) <<8)

        a[0]=a[0] | (value[0] & 0xFF)

       return a 
  }

console.log(intTobytes(1885554121))
var test=new Uint8Array([201, 84, 15, 11])
console.log(bytesToint(intTobytes(1885554121)))

你可能感兴趣的:(javascript uint8数组和uint32之间的转换)