0

I have a C# App that, using WebSockets sends an ArrayBuffer to my Javascript client. The 1st 2 byte are int values (8 bit).

The next 17 bytes represent a string of 17 chars.

The rest is an image array.

Now, I can read the 1st 2 bytes to get my ints. I can also read the image. How do I read the string value which starts from the 2 index (using base index of 1) and the following 17 bytes?

So, my code so far is:

var len = e.data.byteLength;
var dv = new DataView(e.data);
var liveViewIndex = dv.getInt8(0);
var tripped = dv.getInt8(1);
//need to get 17 char string here!!
var frame = e.data.slice(19, len - 19);
desktopImage.src = 'data:image/jpeg;base64,' + base64ArrayBuffer(frame);

thanks

9
  • What is base64ArrayBuffer? Commented Jul 4, 2015 at 17:58
  • Doesn't your slice call work? Commented Jul 4, 2015 at 17:58
  • Hi, it is just a function I found online that converts the ArrayBuffer to base64. It is not part of the question in the real-sense I was just keen show I have done some code. But, will post function if you like? :) Commented Jul 4, 2015 at 17:59
  • Slice - yes it does. My image appears so it must do Commented Jul 4, 2015 at 17:59
  • 1
    Yeah, sure, but I meant the string encoding. Is it an ASCII string? UTF-8? Something else? You might be looking for StringView Commented Jul 4, 2015 at 18:06

1 Answer 1

2

Given that the string is encoded as simple ASCII, the following should work:

var string = String.fromCharCode.apply(String, new Uint8Array(e.data, 2, 17));

It first creates a Uint8Array view on the interesting part of the buffer, and then uses it as arguments to String.fromCharCode.

Sign up to request clarification or add additional context in comments.

5 Comments

Uh, wait, do you mean this code gave you a bluescreen?
lol no! It was not that potent. I am testing with a micro PC and it gets too hot in this weather and so needs rebooting every now and again. I did not want you to think I was rude by not replying in a timely fashion :)
Oh, really no offense taken - SO comments are not a real-time chat (even though responses can come really fast). Take your time.
works for me - thanks. I shall repost the question relating to converting ArrayBuffer to Blob using DataView.. Thanks for your answer
So far I can only tell you that the Blob constructor will take buffers as argument, though afaik it doesn't do any base64 encoding for you. But yes, this is really better asked as a second question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.