Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can I make Javascript see binary data?

2 views
Skip to first unread message

pete

unread,
Jul 30, 2010, 4:26:30 PM7/30/10
to

Hi everybody --

A friend asks me this question. He is reading mixed ASCII and binary
data into his Javascript code. He needs to parse the received record.
Some of the binary data are:

1. 16-bit integers
2. 32-bit integers
3. 8-bit integers
4. 32-bit IEEE floats.

So I think the question is: can I persuade Javascript to understand
and handle 8 arbitrary data bits in an untyped 8-bit byte?

If so, then how?

Thanks!

-- pete

Ry Nohryb

unread,
Jul 30, 2010, 5:52:38 PM7/30/10
to

Not in a browser, no. Not yet.
--
Jorge.

Denis McMahon

unread,
Jul 30, 2010, 6:02:34 PM7/30/10
to

bitwise or, bitwise and etc.

eg (assuming bit 0 is lsb):

if (x & 0x80 == 0x80) alert ("bit 7 is set");
if (x | 0x7f == 0x00) alert ("bit 7 is clear");

x = (x | 0x80); // set bit 7 of x
x = (x & 0x7f); // clear bit 7 of x

Rgds

Denis McMahon

pete

unread,
Jul 30, 2010, 6:27:51 PM7/30/10
to
On Jul 30, 6:02 pm, Denis McMahon <denis.m.f.mcma...@googlemail.com>
wrote:

Right, but also (considering the input stream as an array of bytes):

var a = stream[i];
a <<= 8;
a += stream[i+1];

-- pete

Denis McMahon

unread,
Jul 31, 2010, 2:21:51 PM7/31/10
to
On 30/07/10 23:27, pete wrote:
> On Jul 30, 6:02 pm, Denis McMahon <denis.m.f.mcma...@googlemail.com>
> wrote:
>> On 30/07/10 21:26, pete wrote:
>>
>>> Hi everybody --
>>
>>> A friend asks me this question. He is reading mixed ASCII and binary
>>> data into his Javascript code. He needs to parse the received record.
>>> Some of the binary data are:
>>
>>> 1. 16-bit integers
>>> 2. 32-bit integers
>>> 3. 8-bit integers
>>> 4. 32-bit IEEE floats.
>>
>>> So I think the question is: can I persuade Javascript to understand
>>> and handle 8 arbitrary data bits in an untyped 8-bit byte?
>>
>>> If so, then how?
>>
>> bitwise or, bitwise and etc.
>>
>> eg (assuming bit 0 is lsb):
>>
>> if (x & 0x80 == 0x80) alert ("bit 7 is set");
>> if (x | 0x7f == 0x00) alert ("bit 7 is clear");
>>
>> x = (x | 0x80); // set bit 7 of x
>> x = (x & 0x7f); // clear bit 7 of x

> Right, but also (considering the input stream as an array of bytes):


>
> var a = stream[i];
> a <<= 8;
> a += stream[i+1];

No idea, outside of my knowledge.

Rgds

Denis McMahon

0 new messages