Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:47:21+00:00 2026-06-01T11:47:21+00:00

I send binary data over websocket from python server: data = struct.pack(‘!BI’, 2, 1)

  • 0

I send binary data over websocket from python server:

data = struct.pack('!BI', 2, 1)
channel.send(channel.BINARY, data)

Data sending works fine.
On client side javascript gets it like this:

onbinarydata: function(data) {
    alert(data.byteLength);
    >> 5
    var array = new Uint8Array(data,0,1);
    alert(array[0]);
    >> 2
    var array2 = new Uint32Array(data,1,5);
    >> Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1
    alert(array2[0]);

How can this happen?
Byte length minus byte offset is 4, and BYTY_ELEMENT_SIZE is also 4.
First value is read without problems, but next always raise error, whatever type it is.
Even if I only set byte offset like this

var array2 = new Uint32Array(data,1);

there is an error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:47:23+00:00Added an answer on June 1, 2026 at 11:47 am

    One cannot create int32/uint32 array with offset not divisible by 4. The same for int16/uint16 and 2.

    This is the limitation of CPU architecture.

    Let those 2 variables would be A:Uint16 and B:Uint8. You can map them onto bytes with different methods:

    • BBAA (put value B into Uint16)
    • 0BAA / B0AA (skip 1 byte for alignment)
    • AAB (reorder for alignment).

    If you don’t want to think about big-/little-endiad, reordering and other stuff… i.e. if you still want to put variables into bytes as BAA for some reason, then use the following idea:

    For each not-aligned variable put into corresponding size buffer and copy it byte-by-byte.
    If misalign is 2 bytes, then copy may be made by pair of bytes.

    var array = new Uint8Array(data,0,1);
    alert(array);
    
    var array2 = new Uint8Array(data,1,5);
    var abuf2 = new ArrayBuffer(4);
    var abuf2_8 = new Uint8Array(abuf2);
    // using `subarray` won't help, cause it doesn't create a copy like `slice`
    // and the same data will have the same inconsistent memory alignment
    for (var i = 0; i < 4; i++) abuf2_8[i] = array2[i];
    var abuf2_32 = new Uint32Array(abuf2);
    alert(abuf2_32[0]);
    

    BTW There’s some ready code for packing/unpacking in js:

    • mine
    • phpjs

    which are similar to php pack

    My code may be more interesting to you, cause there’re 2 classes: to pack into binary string and to pack into arraybuffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send large binary(2Gb-10Gb) data from one pc(client) to another pc(server) over
I'm trying to send binary data from a database over an ASP.NET page. I
I send binary data from client (Debian 6.0.3) to server (Windows Server 2003). To
I am trying to send Binary data using python raw socket. For that i
I can send binary data from a Restlet Client like so Representation representation =
I have been sending binary data between applications lots of times over TCP sockets
I want to send some binary data over via GET using the Indy components.
I have an simple understanding question: Why can I only send Binary Data from
I send an email from my smtp server, for example, setting From to anyemail@anydomain.com
I have WCF service that is IIS-hosted and I have to send binary data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.