14

I am doing server-side javascript and i need to have a typed array of byte of a certain size. I tried :

var buf = [1024]; (guives me Cannot convert org.mozilla.javascript.NativeArray@1e565bd to byte[] error)
var buf = byte[1024]; (wrong synthax)

What is the synthax?

6
  • 6
    There is no "byte" type in JavaScript. There are, however, "typed arrays" in very new versions of JavaScript, supported in new browsers and in V8. What you want is an "Int8Array" or a "Uint8Array". Commented Jun 14, 2012 at 1:01
  • @Pointy do i just declare : var buf = new Int8Array() ? Commented Jun 14, 2012 at 1:05
  • @Gab khronos.org/registry/typedarray/specs/latest Commented Jun 14, 2012 at 1:07
  • 1
    The typed arrays are really pretty weird; they're somewhat different from normal JavaScript arrays. Commented Jun 14, 2012 at 1:09
  • @ephemient i am not using recent enough version of JavaScript to have any of what's described in khronos.org/registry/typedarray/specs/latest Commented Jun 14, 2012 at 13:38

1 Answer 1

7

This depends on which server-side JavaScript package you use. Different packages implement different flavors of JavaScript and different versions of ECMAScript.

In NodeJS v0.6.x you have access to typed arrays. Creating one of these arrays is fairly trivial.

// creating an array of bytes, with 1024 elements
var bytes = new Uint8Array(1024);

There are other typed arrays available, handling 16 bit and 32 bit integers.

// creating an array of 16 bit integers, with 128 elements
var array_16bit = new Uint16Array(128);

// creating an array of 32 bit integers, with 16 elements
var array_32bit = new Uint32Array(16);

When using typed arrays, there are a few things to keep in mind. Typed arrays do not inherit the standard array prototype, and these arrays have an immutable length.

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

Comments

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.