最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I read out the first 4 bytes in javascript, turn it into an integer and remove the rest? - Stack Overflow

programmeradmin1浏览0评论

I need to transfer webcam data over the internet from one browser to another.

The webcam is displayed in a HTML5 canvas. Then I get its dataUrl, and turn it into a blob. Then I send this blob to my server.

From what I understand, the blob is essentially a byte array. I've converted it to a byte array on the server-side, it has the same length as blob.size in the browser, so that seems fine. I need to add a sender id to it, so I convert an integer to an array of 4 bytes and add it to the front of the byte array.

Now I need to send this modified blob to the other browser. And this is where I'm confused.

  1. How do I read out the first 4 bytes in javascript and turn it into an integer again?
  2. And how do I cut off the rest of the blob?

I need to transfer webcam data over the internet from one browser to another.

The webcam is displayed in a HTML5 canvas. Then I get its dataUrl, and turn it into a blob. Then I send this blob to my server.

From what I understand, the blob is essentially a byte array. I've converted it to a byte array on the server-side, it has the same length as blob.size in the browser, so that seems fine. I need to add a sender id to it, so I convert an integer to an array of 4 bytes and add it to the front of the byte array.

Now I need to send this modified blob to the other browser. And this is where I'm confused.

  1. How do I read out the first 4 bytes in javascript and turn it into an integer again?
  2. And how do I cut off the rest of the blob?
Share Improve this question edited Apr 17, 2014 at 17:55 Braiam 4,49611 gold badges49 silver badges83 bronze badges asked Apr 17, 2014 at 17:48 spacecoyotespacecoyote 2,0493 gold badges20 silver badges25 bronze badges 1
  • 1 use the parseInt(someInt, base); for turning 4bytes into an integer – Callum Linington Commented Apr 17, 2014 at 17:51
Add a ment  | 

1 Answer 1

Reset to default 7

You can use Blob.slice() method to get bytes.
See docs: https://developer.mozilla/en-US/docs/Web/API/Blob.slice

1)

var bytes = blob.slice(0,4);

2)

var arrayOfBytes = [];
for(var i=0;i<blob.size/4;i++){
    arrayOfBytes[i] = blob.slice(i*4,4+(i*4));
}

Note: I didn't test it!

发布评论

评论列表(0)

  1. 暂无评论