Read a Int64 in an ArrayBuffer using DataView












2















I am using Web Sockets.



I am passing an ArrayBuffer to my JavaScript page.



I have this in C# code:



 byte packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;

byte tickArray = BitConverter.GetBytes( 635744635349556838 );
byte packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);


In my JavaScript Client I have this:



 var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);

var x = dv.getInt8(2);


I get the results:



1
0
-29


if change to this:



var x= dv.getInt16(1);

-7389


How do I get my value of 635744635349556838?



Thanks










share|improve this question























  • actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

    – Hacketo
    Aug 6 '15 at 13:56













  • Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

    – Andrew Simpson
    Aug 6 '15 at 13:59











  • FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

    – robertklep
    Aug 6 '15 at 14:09











  • @robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

    – Andrew Simpson
    Aug 6 '15 at 14:22
















2















I am using Web Sockets.



I am passing an ArrayBuffer to my JavaScript page.



I have this in C# code:



 byte packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;

byte tickArray = BitConverter.GetBytes( 635744635349556838 );
byte packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);


In my JavaScript Client I have this:



 var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);

var x = dv.getInt8(2);


I get the results:



1
0
-29


if change to this:



var x= dv.getInt16(1);

-7389


How do I get my value of 635744635349556838?



Thanks










share|improve this question























  • actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

    – Hacketo
    Aug 6 '15 at 13:56













  • Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

    – Andrew Simpson
    Aug 6 '15 at 13:59











  • FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

    – robertklep
    Aug 6 '15 at 14:09











  • @robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

    – Andrew Simpson
    Aug 6 '15 at 14:22














2












2








2








I am using Web Sockets.



I am passing an ArrayBuffer to my JavaScript page.



I have this in C# code:



 byte packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;

byte tickArray = BitConverter.GetBytes( 635744635349556838 );
byte packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);


In my JavaScript Client I have this:



 var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);

var x = dv.getInt8(2);


I get the results:



1
0
-29


if change to this:



var x= dv.getInt16(1);

-7389


How do I get my value of 635744635349556838?



Thanks










share|improve this question














I am using Web Sockets.



I am passing an ArrayBuffer to my JavaScript page.



I have this in C# code:



 byte packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;

byte tickArray = BitConverter.GetBytes( 635744635349556838 );
byte packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);


In my JavaScript Client I have this:



 var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);

var x = dv.getInt8(2);


I get the results:



1
0
-29


if change to this:



var x= dv.getInt16(1);

-7389


How do I get my value of 635744635349556838?



Thanks







javascript arraybuffer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 6 '15 at 13:40









Andrew SimpsonAndrew Simpson

2,818739107




2,818739107













  • actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

    – Hacketo
    Aug 6 '15 at 13:56













  • Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

    – Andrew Simpson
    Aug 6 '15 at 13:59











  • FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

    – robertklep
    Aug 6 '15 at 14:09











  • @robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

    – Andrew Simpson
    Aug 6 '15 at 14:22



















  • actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

    – Hacketo
    Aug 6 '15 at 13:56













  • Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

    – Andrew Simpson
    Aug 6 '15 at 13:59











  • FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

    – robertklep
    Aug 6 '15 at 14:09











  • @robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

    – Andrew Simpson
    Aug 6 '15 at 14:22

















actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

– Hacketo
Aug 6 '15 at 13:56







actually with javascript 635744635349556838 == 635744635349556900 if you can send it as string. What does e.data output with a console.log ?

– Hacketo
Aug 6 '15 at 13:56















Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

– Andrew Simpson
Aug 6 '15 at 13:59





Hi, yes, I was looking into doing that. This number I am using is the UTC tick of the server. I am assuming the length can change over time. Also, this byte array is put at the beginning of a larger byte array before being sent to the client(s). It is this varying length of string that will task me

– Andrew Simpson
Aug 6 '15 at 13:59













FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

– robertklep
Aug 6 '15 at 14:09





FWIW, your value exceeds Number.MAX_SAFE_INTEGER.

– robertklep
Aug 6 '15 at 14:09













@robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

– Andrew Simpson
Aug 6 '15 at 14:22





@robertklep Thanks, I came to the same result myself. I had to convert number to string and then use the length of as my header in int16

– Andrew Simpson
Aug 6 '15 at 14:22












1 Answer
1






active

oldest

votes


















1














I wrote a code. Would be it helpful?



function getUint64(bytes, littleEndian)
{
var low = 4, high = 0;
if (littleEndian)
{
low = 0;
high = 4;
}

var dv = new DataView(Uint8Array.from(bytes ).buffer);

return (dv.getUint32(high, littleEndian) << 32) |
dv.getUint32(low, littleEndian);
}
var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
var value = getUint64(bytes, false);
console.log(value);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31857374%2fread-a-int64-in-an-arraybuffer-using-dataview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I wrote a code. Would be it helpful?



    function getUint64(bytes, littleEndian)
    {
    var low = 4, high = 0;
    if (littleEndian)
    {
    low = 0;
    high = 4;
    }

    var dv = new DataView(Uint8Array.from(bytes ).buffer);

    return (dv.getUint32(high, littleEndian) << 32) |
    dv.getUint32(low, littleEndian);
    }
    var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
    var value = getUint64(bytes, false);
    console.log(value);





    share|improve this answer




























      1














      I wrote a code. Would be it helpful?



      function getUint64(bytes, littleEndian)
      {
      var low = 4, high = 0;
      if (littleEndian)
      {
      low = 0;
      high = 4;
      }

      var dv = new DataView(Uint8Array.from(bytes ).buffer);

      return (dv.getUint32(high, littleEndian) << 32) |
      dv.getUint32(low, littleEndian);
      }
      var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
      var value = getUint64(bytes, false);
      console.log(value);





      share|improve this answer


























        1












        1








        1







        I wrote a code. Would be it helpful?



        function getUint64(bytes, littleEndian)
        {
        var low = 4, high = 0;
        if (littleEndian)
        {
        low = 0;
        high = 4;
        }

        var dv = new DataView(Uint8Array.from(bytes ).buffer);

        return (dv.getUint32(high, littleEndian) << 32) |
        dv.getUint32(low, littleEndian);
        }
        var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
        var value = getUint64(bytes, false);
        console.log(value);





        share|improve this answer













        I wrote a code. Would be it helpful?



        function getUint64(bytes, littleEndian)
        {
        var low = 4, high = 0;
        if (littleEndian)
        {
        low = 0;
        high = 4;
        }

        var dv = new DataView(Uint8Array.from(bytes ).buffer);

        return (dv.getUint32(high, littleEndian) << 32) |
        dv.getUint32(low, littleEndian);
        }
        var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
        var value = getUint64(bytes, false);
        console.log(value);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 19:23









        JQ22JQ22

        113




        113
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31857374%2fread-a-int64-in-an-arraybuffer-using-dataview%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga