简体   繁体   中英

Convert string to Uint8Array

I have string like below

b/dWBWBYDdE2A5Uh , i want to convert to

Uint8Array(12) [111, 247, 86, 5, 96, 88, 13, 209, 54, 3, 149, 33, buffer: ArrayBuffer(12), byteLength: 12, byteOffset: 0, length: 12, Symbol(Symbol.toStringTag): 'Uint8Array']

So I tried something like below

new TextEncoder("utf-8").encode("b/dWBWBYDdE2A5Uh");

But I get

Uint8Array(16) [98, 47, 100, 87, 66, 87, 66, 89, 68, 100, 69, 50, 65, 53, 85, 104, buffer: ArrayBuffer(16), byteLength: 16, byteOffset: 0, length: 16, Symbol(Symbol.toStringTag): 'Uint8Array'] ,

can some one help me to solve this encryption, thanks.

This was done by 3rd party website, so i cam trying to decode it.

Looks like b/dWBWBYDdE2A5Uh is the base-64 encoded version of the given 12 bytes of binary data. You can decode base-64 with atob , but it gives you a string and not an Uint8Array . Each character in the string represents one byte.

There is no built-in function to convert this string to an Uint8Array , but we can do it, as adapted from this answer :

>> Uint8Array.from(atob('b/dWBWBYDdE2A5Uh'), c => c.charCodeAt(0))
Uint8Array(12) [ 111, 247, 86, 5, 96, 88, 13, 209, 54, 3, … ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM