Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: How do I convert the following to Uint8Array. Date: Sat, 23 Jan 2016 11:17:16 +0000 Organization: A noiseless patient Spider Lines: 31 Message-ID: <87y4bg4kar.fsf@bsb.me.uk> References: <73ad0ab9-a27b-4c72-b111-58047bc27f45@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="32495"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oouw5O2myRvLorxz11Y2+UchVXjvp+68=" Cancel-Lock: sha1:65Y8rnLwkRiZxZZEapasj+EXS9M= sha1:BI6Ol4YeUh25GmB6O4jVAB0XCew= X-BSB-Auth: 1.50731f1cb916e4ad56bc.20160123111716GMT.87y4bg4kar.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:29415 Robby Balona writes: > On Friday, January 22, 2016 at 1:52:47 PM UTC+2, 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]); > 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.