Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29402 > unrolled thread

How do I convert the following to Uint8Array.

Started byRobby Balona <robby@grab.co.za>
First post2016-01-22 03:52 -0800
Last post2016-01-24 09:44 -0800
Articles 10 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  How do I convert the following to Uint8Array. Robby Balona <robby@grab.co.za> - 2016-01-22 03:52 -0800
    Re: How do I convert the following to Uint8Array. Stefan Weiss <krewecherl@gmail.com> - 2016-01-22 13:25 +0100
      Re: How do I convert the following to Uint8Array. Stefan Weiss <krewecherl@gmail.com> - 2016-01-22 13:32 +0100
        Re: How do I convert the following to Uint8Array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-22 12:54 +0000
          Re: How do I convert the following to Uint8Array. Stefan Weiss <krewecherl@gmail.com> - 2016-01-22 14:22 +0100
    Re: How do I convert the following to Uint8Array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-22 12:41 +0000
    Re: How do I convert the following to Uint8Array. Robby Balona <robby@grab.co.za> - 2016-01-23 00:51 -0800
      Re: How do I convert the following to Uint8Array. Stefan Weiss <krewecherl@gmail.com> - 2016-01-23 12:15 +0100
      Re: How do I convert the following to Uint8Array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-23 11:17 +0000
    Re: How do I convert the following to Uint8Array. Robby Balona <robby@grab.co.za> - 2016-01-24 09:44 -0800

#29402 — How do I convert the following to Uint8Array.

FromRobby Balona <robby@grab.co.za>
Date2016-01-22 03:52 -0800
SubjectHow do I convert the following to Uint8Array.
Message-ID<ce4bb039-aa19-4cbe-bb9b-8b8f004f8992@googlegroups.com>
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

[toc] | [next] | [standalone]


#29403

FromStefan Weiss <krewecherl@gmail.com>
Date2016-01-22 13:25 +0100
Message-ID<n7t74i$obt$1@news.albasani.net>
In reply to#29402
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

[toc] | [prev] | [next] | [standalone]


#29404

FromStefan Weiss <krewecherl@gmail.com>
Date2016-01-22 13:32 +0100
Message-ID<n7t7hq$p6i$1@news.albasani.net>
In reply to#29403
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

[toc] | [prev] | [next] | [standalone]


#29406

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-22 12:54 +0000
Message-ID<87powtwz8x.fsf@bsb.me.uk>
In reply to#29404
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.

[toc] | [prev] | [next] | [standalone]


#29407

FromStefan Weiss <krewecherl@gmail.com>
Date2016-01-22 14:22 +0100
Message-ID<n7taev$uh4$1@news.albasani.net>
In reply to#29406
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

[toc] | [prev] | [next] | [standalone]


#29405

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-22 12:41 +0000
Message-ID<87y4bhwzvm.fsf@bsb.me.uk>
In reply to#29402
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.

[toc] | [prev] | [next] | [standalone]


#29413

FromRobby Balona <robby@grab.co.za>
Date2016-01-23 00:51 -0800
Message-ID<73ad0ab9-a27b-4c72-b111-58047bc27f45@googlegroups.com>
In reply to#29402
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

[toc] | [prev] | [next] | [standalone]


#29414

FromStefan Weiss <krewecherl@gmail.com>
Date2016-01-23 12:15 +0100
Message-ID<n7vnc5$ivv$1@news.albasani.net>
In reply to#29413
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

[toc] | [prev] | [next] | [standalone]


#29415

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-23 11:17 +0000
Message-ID<87y4bg4kar.fsf@bsb.me.uk>
In reply to#29413
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.

[toc] | [prev] | [next] | [standalone]


#29431

FromRobby Balona <robby@grab.co.za>
Date2016-01-24 09:44 -0800
Message-ID<964eaf69-5aa6-4d3a-abb3-4a31dc42dffc@googlegroups.com>
In reply to#29402
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web