javascript - How can I instantiate an ArrayBuffer from a hexadecimal representation of an octet stream? -


i have binary information coming on tcp connection (websocket). traced console in encoded format so:

53 54 41 52 54 45 44 3a 31 34 32 38 36 30 32 30 38 37  

i presume hex encoding of each of bytes.

the information protocol buffer information. write function decode using library have. first step me create buffer object of kind encapsulate binary information supply library.

i not yet know precise type expectation of library, expects binary buffer of sort.

the protocol buffer decoding library api looks so:

library.bytebuffertoresponse(buffer); 

how can instantiate "binary buffer" of kind hexadecimal representation of octet stream?

var octetstream = '34 36 10 04 1a 05 0a 01 30'; var arraybuffer = new arraybuffer(); // how can initialize binary data? 

you'll want use typed array access buffer. can directly put array literal have constructor, construct buffer or appropriate size.

like body=53 54 41 52 54 45 44 3a 31 34 32 38 36 30 32 30 38 37 37 38 36 interpret hex encoded bytes.

for that, can use typedarray.from map function:

var msg = "body=53 54 41 52 54 45 44 3a 31 34 32 38 36 30 32 30 38 37 37 38 36"; var arr = uint8array.from(msg.slice(5).split(" "), function(byte) {     return parseint(byte, 16); }); var buffer = arr.buffer; // not sure need 

Popular posts from this blog