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


Groups > comp.lang.javascript > #29406

Re: How do I convert the following to Uint8Array.

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: How do I convert the following to Uint8Array.
Date Fri, 22 Jan 2016 12:54:38 +0000
Organization A noiseless patient Spider
Lines 35
Message-ID <87powtwz8x.fsf@bsb.me.uk> (permalink)
References <ce4bb039-aa19-4cbe-bb9b-8b8f004f8992@googlegroups.com> <n7t74i$obt$1@news.albasani.net> <n7t7hq$p6i$1@news.albasani.net>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="22131"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dueh4gPdX11BJUdaPky4h6870NXwFB/M="
Cancel-Lock sha1:X0XAeih4yQVyWFpC1sV+wjs5/3g= sha1:Ok/lEGNGc2QyYMpsMlABNDoLCmw=
X-BSB-Auth 1.540820119510ae70a32b.20160122125438GMT.87powtwz8x.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:29406

Show key headers only | View raw


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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web