Ultra
Ultra

Reputation: 57

Convert Uint16Array containing 8-bit integers to regular integer

How do you convert following

var data = new Uint16Array([131, 220]);

to integer? Expected value is somewhere around 970

Upvotes: 1

Views: 631

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386654

Basically you need to reduce the values by multiplying with 28 and cut off the first bit.

(This is not a general converting, but rather for this special purpose.)

var data = new Uint16Array([131, 220]),
    value = [].reduce.call(data, function (r, a) {
        return (r << 8) + a;
    }, 0) & ((1 << 15) - 1);

console.log(value);

Upvotes: 1

Related Questions