[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

How do I convert the following to Uint8Array.

Robby Balona

1/22/2016 11:53:00 AM

Hi Guys

I am new to javascript. I have been trying to figure this out for a week now and because I don't know what I am talking about .. its hard to Google the right solution.


I have a string "ab05d705" and I am trying to convert it to the following so I can add it to a Uint8Array. So how do I convert the string "ab05d705" to
0xab,0x05,0xd7,0x05 to put into the following
var data = new Uint8Array([0xab,0x05,0xd7,0x05]);


Any help would be so appreciated

Thank you

Robby
9 Answers

Stefan Weiss

1/22/2016 12:26:00 PM

0

On 01/22/2016 12:52, Robby Balona wrote:
> I have a string "ab05d705" and I am trying to convert it to the
> following so I can add it to a Uint8Array. So how do I convert the
> string "ab05d705" to
> 0xab,0x05,0xd7,0x05 to put into the following
> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);

As a one-liner:

str.match(/.{2}/g).map(function (hex) { return parseInt(hex, 16); })

This assumes that the original string is valid as described.
If the performance of this conversion is important, there are other ways
(using one parseInt() and some bit shifting fun).

- stefan

Stefan Weiss

1/22/2016 12:33:00 PM

0

On 01/22/2016 13:25, Stefan Weiss wrote:
> On 01/22/2016 12:52, Robby Balona wrote:
>> I have a string "ab05d705" and I am trying to convert it to the
>> following so I can add it to a Uint8Array. So how do I convert the
>> string "ab05d705" to
>> 0xab,0x05,0xd7,0x05 to put into the following
>> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);
>
> As a one-liner:
>
> str.match(/.{2}/g).map(function (hex) { return parseInt(hex, 16); })

Actually, that didn't answer your question completely. To create a
Uint8Array from your string, you can use:

var str = "ab05d705";
var arr = Uint8Array.from(
str.match(/../g).map(function (hex) { return parseInt(hex, 16); })
);

- stefan

Ben Bacarisse

1/22/2016 12:41:00 PM

0

Robby Balona <robby@grab.co.za> writes:

> I am new to javascript. I have been trying to figure this out for a
> week now and because I don't know what I am talking about .. its hard
> to Google the right solution.
>
> I have a string "ab05d705" and I am trying to convert it to the
> following so I can add it to a Uint8Array. So how do I convert the
> string "ab05d705" to
> 0xab,0x05,0xd7,0x05 to put into the following
> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);

One way:

var s = "ab05d705";
Uint8Array.from(s.replace(/../g, ',0x$&').split(',').slice(1))

Try each part out one at a time to see what this is doing.

--
Ben.

Ben Bacarisse

1/22/2016 12:55:00 PM

0

Stefan Weiss <krewecherl@gmail.com> writes:

> On 01/22/2016 13:25, Stefan Weiss wrote:
>> On 01/22/2016 12:52, Robby Balona wrote:
>>> I have a string "ab05d705" and I am trying to convert it to the
>>> following so I can add it to a Uint8Array. So how do I convert the
>>> string "ab05d705" to
>>> 0xab,0x05,0xd7,0x05 to put into the following
>>> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);
>>
>> As a one-liner:
>>
>> str.match(/.{2}/g).map(function (hex) { return parseInt(hex, 16); })
>
> Actually, that didn't answer your question completely. To create a
> Uint8Array from your string, you can use:
>
> var str = "ab05d705";
> var arr = Uint8Array.from(
> str.match(/../g).map(function (hex) { return parseInt(hex, 16); })
> );

I didn't think of using match -- much nicer! You can avoid using map if
you like because 'from' allows for a mapping function:

Uint8Array.from(s.match(/../g), d => parseInt(d, 16))

or even

Uint8Array.from(s.match(/../g), d => '0x'+d)

but I think the explicit parseInt is more helpful.

--
Ben.

Stefan Weiss

1/22/2016 1:23:00 PM

0

On 01/22/2016 13:54, Ben Bacarisse wrote:
> Stefan Weiss <krewecherl@gmail.com> writes:
>> var str = "ab05d705";
>> var arr = Uint8Array.from(
>> str.match(/../g).map(function (hex) { return parseInt(hex, 16); })
>> );
>
> I didn't think of using match -- much nicer! You can avoid using map if
> you like because 'from' allows for a mapping function:
>
> Uint8Array.from(s.match(/../g), d => parseInt(d, 16))
>
> or even
>
> Uint8Array.from(s.match(/../g), d => '0x'+d)
>
> but I think the explicit parseInt is more helpful.

Ooh, I didn't know about the callback in Uint8Array.from().
That's very useful :)

For the record, here's the bitwise thing I mentioned earlier. It's not
as flexible and rather ugly, but it does the job if the input doesn't
exceed four bytes:

var num = parseInt(str, 16);
var arr = Uint8Array.from([
(num >> 24) & 0xff,
(num >> 16) & 0xff,
(num >> 8) & 0xff,
num & 0xff
]);

- stefan

Robby Balona

1/23/2016 8:52:00 AM

0

On Friday, January 22, 2016 at 1:52:47 PM UTC+2, Robby Balona wrote:
> Hi Guys
>
> I am new to javascript. I have been trying to figure this out for a week now and because I don't know what I am talking about .. its hard to Google the right solution.
>
>
> I have a string "ab05d705" and I am trying to convert it to the following so I can add it to a Uint8Array. So how do I convert the string "ab05d705" to
> 0xab,0x05,0xd7,0x05 to put into the following
> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);
>
>
> Any help would be so appreciated
>
> Thank you
>
> Robby

Thanks everyone for the wonderful help but I get this error when I try to use Uint8Array.from

TypeError: Object function Uint8Array() { [native code] } has no method 'from'

Perhaps I should have also stated that I am using Cordova to do this project .. My bad, should have given more details


Stefan Weiss

1/23/2016 11:15:00 AM

0

On 01/23/2016 09:51, Robby Balona wrote:
> Thanks everyone for the wonderful help but I get this error when I
> try to use Uint8Array.from
>
> TypeError: Object function Uint8Array() { [native code] } has no method 'from'
>
> Perhaps I should have also stated that I am using Cordova to do this
> project .. My bad, should have given more details

OK, I get the same result. Cordova runs on Node.js, and my local Node
version (v0.10.25) also has Uint8Array, but no Uint8Array.from(). It
also doesn't support the ES6 arrow shorthand for functions.

This is an older LTS release from 2 years ago; there have been two more
LTS releases since then - I'm a little surprised that Ubuntu chose to
package this one. You could either upgrade to a newer Node and hope that
its V8 engine supports the new features, or use code that avoids from()
and arrow functions:

var str = "ab05d705";
var arr = new Uint8Array(
str.match(/../g).map(function (hex) { return parseInt(hex, 16); })
);

or

var str = "ab05d705";
var num = parseInt(str, 16);
var arr = new Uint8Array([
(num >> 24) & 0xff,
(num >> 16) & 0xff,
(num >> 8) & 0xff,
num & 0xff
]);

Out of curiosity, why are you using a Uint8Array instead of a plain old
untyped Array?


- stefan

Ben Bacarisse

1/23/2016 11:17:00 AM

0

Robby Balona <robby@grab.co.za> writes:

> On Friday, January 22, 2016 at 1:52:47 PM UTC+2, Robby Balona wrote:
<snip>
>> I have a string "ab05d705" and I am trying to convert it to the
>> following so I can add it to a Uint8Array. So how do I convert the
>> string "ab05d705" to
>> 0xab,0x05,0xd7,0x05 to put into the following
>> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);
<snip>
> Thanks everyone for the wonderful help but I get this error when I try
> to use Uint8Array.from
>
> TypeError: Object function Uint8Array() { [native code] } has no method 'from'
>
> Perhaps I should have also stated that I am using Cordova to do this
> project .. My bad, should have given more details

Most of the solutions can be altered to avoid using Uint8Array.from.
For example, s.match(/../g).map(s => parseInt(s, 16)) simply makes an
array of numbers which could be used as in your own example:

var data = new Uint8Array(s.match(/../g).map(s => parseInt(s, 16)));

I don't know Cordova, so you might also run into trouble with the "arrow
function" syntax. In which case, you need the more wordy

s.match(/../g).map(function (s) { return parseInt(s, 16); })

--
Ben.

Robby Balona

1/24/2016 5:45:00 PM

0

On Friday, January 22, 2016 at 1:52:47 PM UTC+2, Robby Balona wrote:
> Hi Guys
>
> I am new to javascript. I have been trying to figure this out for a week now and because I don't know what I am talking about .. its hard to Google the right solution.
>
>
> I have a string "ab05d705" and I am trying to convert it to the following so I can add it to a Uint8Array. So how do I convert the string "ab05d705" to
> 0xab,0x05,0xd7,0x05 to put into the following
> var data = new Uint8Array([0xab,0x05,0xd7,0x05]);
>
>
> Any help would be so appreciated
>
> Thank you
>
> Robby




Ben .. you are my hero ......I cannot thank you enough... This is what i ended up with in the end



var data = new Uint8Array(s.match(/../g).map(function (s) {
return parseInt(s, 16);
}));

You Rock Dude.. thank you again everyone