I have a binary string that must travel over a ws websocket (I can't use websocket.io) and so is being JSON.stringified. eg var msg.data = data.toString('base64')
On the other end, I want that data back not as byte wide binary, but as an array of 32 bit integers. eg if the binary data is [0, 0, 0, 1] going in, I want [1] coming out. Each output element is 4 bytes.
If I just take the binary string directly, I can new Int32Array(data)
and I'm golden; the result is 1/4 the length of the original and each 32 bit element is made up from 4 of the original byte wide elements.
But when I've encoded it, then decoded with var data = Buffer.from(msg.data, 'base64')
then new Int32Array(data)
is the same length as the original, and each 32 byte element is made from ONE of the original 8 byte elements. Int32Array.from(data)
does the same.
I'm not finding any answer by searching, everyone appears to be ok with byte wide data.
.buffer
I was forgetting .buffer.
new Int32Array(data.buffer)
works perfectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.