Dan
Dan

Reputation: 2344

Create Byte Array in Javascript form Java

I am in the process of converting some Java to JavaScript and have run across a mismatch that I can't figure out

Java:

byte ex[] = new byte[(38+5+10+62)/32*32-38];

My attempted conversion in JavaScript is:

var ex = new Int8Array((38+5+10+62)/32*32-38);

The length of the Java array is 58

The length of the JavaScript array is 77

I assume it's either the calculation itself that needs to be process differently or the Int8Array that needs to be something else.

Any ideas?

Upvotes: 1

Views: 318

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138277

As pointed out in the comments, this is due to differences in the way how numbers are handled. JavaScript uses "numbers" (64bit floating-point), while Java distinguishes different primitive number types, and in this case integers are used, which means that /32*32 effectively rounds the value down to the nearest 32 implicitly, in js that has to be done explicitly:

 Math.floor((38+5+10+62)/ 32 ) * 32 -38

Upvotes: 1

Related Questions