Pack.writeInt()

1 view
Skip to first unread message

Phil Rhodes

unread,
Dec 14, 2009, 5:24:53 PM12/14/09
to jsl...@googlegroups.com
Hi,
 
I'm having trouble writing 8-byte ints (QWORDs) to Pack instances:
 
var foo = new Buffer();
var bar = new Pack(Buffer);
var largeNumber = Math.pow(2^63);
bar.writeInt(largeNumber,8);
 
> struct3.js:123: Error: Unable to manage this size.
 
This does not occur with four-byte ints.
 
I'm writing a custom chunk into a RIFF WAVE file which includes a time reference value as a number of audio samples in <24 hours. Since the number of 48KHz PCM audio samples in up-to-24 hours may be very large, the format uses a QWORD.
 
P

soubok

unread,
Dec 14, 2009, 6:00:02 PM12/14/09
to jslibs
Hi,
Tracemonkey is not able to manage 64bit integers natively. However, a
big integer can be stored in a double data type without loosing
precision using the mantissa bits only.
This mean that 2^53 will be the higher integer value you will be able
to manage in pure JavaScript.
If it is not suitable, the only alternative will be to split your
64bit integer in two 32bit integers.

Franck.

Phil Rhodes

unread,
Dec 14, 2009, 6:14:44 PM12/14/09
to jsl...@googlegroups.com
The maximum (sane) value for this field is 16588800000.

I don't know if I can get away with that :)

P

Phil Rhodes

unread,
Dec 14, 2009, 6:30:17 PM12/14/09
to jsl...@googlegroups.com
Here's what I came up with, anyway:


if (this.byteLen === 8) {
packer.writeInt(this.value >> 32, 4); // big
packer.writeInt(this.value & 0xFFFFFFFF, 4); // little
} else {
packer.writeInt(this.value, this.byteLen);
}

soubok

unread,
Dec 15, 2009, 7:02:45 AM12/15/09
to jsl...@googlegroups.com
On Tue, Dec 15, 2009 at 00:14, Phil Rhodes <phil_...@rocketmail.com> wrote:
> The maximum (sane) value for this field is 16588800000.

I can support integers from -9007199254740991 to +9007199254740991

>
> I don't know if I can get away with that :)
>
> P
>
> --
> Group jslibs - http://groups.google.com/group/jslibs - jsl...@googlegroups.com
> Unsubscribe: jslibs-un...@googlegroups.com

Phil Rhodes

unread,
Dec 15, 2009, 8:14:21 AM12/15/09
to jsl...@googlegroups.com
> I can support integers from -9007199254740991 to +9007199254740991

Great!

I think that writeInt(n,8) should be supported, anyway, even if there's a
possible lack of precision.

P

soubok

unread,
Dec 16, 2009, 1:46:53 PM12/16/09
to jsl...@googlegroups.com
FYI, The modification has been committed in the trunk.

Phil Rhodes

unread,
Dec 16, 2009, 3:35:21 PM12/16/09
to jsl...@googlegroups.com
We're going to have to buy this guy a christmas present...

P
--------------------------------------------------------------------------------



No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.427 / Virus Database: 270.14.110/2568 - Release Date: 12/16/09
08:02:00

Phil Rhodes

unread,
Dec 16, 2009, 5:58:51 PM12/16/09
to jsl...@googlegroups.com

> FYI, The modification has been committed in the trunk.

Just out of interest - does this enable writing of ints of arbitrary length,
or just 8?

For instance, in wave files, any sample between 9 and 24 bits is packed into
a three-byte word - can I write three-byte words now?

P

soubok

unread,
Dec 16, 2009, 7:22:02 PM12/16/09
to jsl...@googlegroups.com
On Wed, Dec 16, 2009 at 23:58, Phil Rhodes <phil_...@rocketmail.com> wrote:
>
>> FYI, The modification has been committed in the trunk.
>
> Just out of interest - does this enable writing of ints of arbitrary length,
> or just 8?

Currently I support 1,2,4,8 Bytes

>
> For instance, in wave files, any sample between 9 and 24 bits is packed into
> a three-byte word - can I write three-byte words now?

I will have a look at 3 bytes support.

Phil Rhodes

unread,
Dec 16, 2009, 8:44:43 PM12/16/09
to jsl...@googlegroups.com
>> For instance, in wave files, any sample between 9 and 24 bits is packed
>> into
>> a three-byte word - can I write three-byte words now?
>
> I will have a look at 3 bytes support.

Sorry... I feel like I keep throwing jobs at you!

P

Reply all
Reply to author
Forward
0 new messages