How to build a UCS-2 encoded HEX string from a Javascript "basic" string, but only using web api (browser or Deno) features ?
In other words, how to convert the following node code but for the browser ?
message = "hello world "
const buf_le = Buffer.from(message, "ucs2"); // 'ucs2' is as same as 'utf16le'
let buf = Buffer.alloc(buf_le.length); // convert to utf16be (ucs2)
for (let i = 0; i < buf.length; i += 2)
buf.writeUInt16BE(buf_le.readUInt16LE(i), i);
hex = buf.toString("hex");
It will be used to encode text to PDU for SMS sendings.
Thanks!
You can use this solution from How to convert ArrayBuffer to and from String - Chrome Developers (usage of
var
updated to useconst
orlet
):It uses a
Uint16Array
as the view into theArrayBuffer
. "TheUint16Array
typed array represents an array of 16-bit unsigned integers in the platform byte order. If control over byte order is needed, useDataView
instead."To get a hex representation there are several options:
view the
buf
using aUint16Array
instance so that you don't need to concern yourself with your platform's byte order:update
str2ab
to use aDataView
instead ofUint16Array
so that you can write directly to network byte order (big endian):Keep
str2ab
as-is from Chrome Developers and then convert from platform endianness to big-endian by reading from thebuf
usingUint16Array
and writing to a buffer using aDataView
like seen above.