uint8-to-utf16

0.3.0 • Public • Published

A safe Uint8Array to utf-16 string converter

Build Status Coverage Status

Social Media Photo by Suzanne D. Williams on Unsplash

Compatible with any binary data and every modern, client/server, JS engine.

import {encode, decode} from 'uint8-to-utf16';
// const {encode, decode} = require('uint8-to-utf16');
 
const utf8Binary = new Uint8Array(anyArrayBuffer);
 
// encode converts Uint8Array instances to utf-16 strings
const encoded = encode(utf8Binary);
 
// it's just like any other strings
console.log(encoded);
 
// decode converts utf-16 strings, encoded via this module,
// into their original Uint8Array representation
const decoded = decode(encoded);
 
console.assert(
  JSON.stringify([...utf8Binary]) ===
  JSON.stringify([...decoded]),
  'safe Uint8Array to utf-16 conversion'
);

Please note if you use TextEncoder or TextDecoder your encoded string might lose important data in the process.

Looking for a drop in module that converts into base64 strings instead? Check uint8-to-base64 out!

Package Sidebar

Install

npm i uint8-to-utf16

Weekly Downloads

95

Version

0.3.0

License

ISC

Unpacked Size

7.02 kB

Total Files

9

Last publish

Collaborators

  • webreflection