Read a Int64 in an ArrayBuffer using DataView
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
add a comment |
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
actually with javascript635744635349556838 == 635744635349556900
if you can send it as string. What doese.data
output with aconsole.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 exceedsNumber.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
add a comment |
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
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
javascript arraybuffer
asked Aug 6 '15 at 13:40
Andrew SimpsonAndrew Simpson
2,818739107
2,818739107
actually with javascript635744635349556838 == 635744635349556900
if you can send it as string. What doese.data
output with aconsole.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 exceedsNumber.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
add a comment |
actually with javascript635744635349556838 == 635744635349556900
if you can send it as string. What doese.data
output with aconsole.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 exceedsNumber.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
add a comment |
1 Answer
1
active
oldest
votes
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 25 '18 at 19:23
JQ22JQ22
113
113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
actually with javascript
635744635349556838 == 635744635349556900
if you can send it as string. What doese.data
output with aconsole.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