From: pete on

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



From: Ry Nohryb on
On Jul 30, 10:26 pm, pete <pete...(a)yahoo.com> 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?

Not in a browser, no. Not yet.
--
Jorge.
From: Denis McMahon on
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

Rgds

Denis McMahon
From: pete on
On Jul 30, 6:02 pm, Denis McMahon <denis.m.f.mcma...(a)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
>
> Rgds
>
> Denis McMahon

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

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

-- pete
From: Denis McMahon on
On 30/07/10 23:27, pete wrote:
> On Jul 30, 6:02 pm, Denis McMahon <denis.m.f.mcma...(a)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