utf8-to-bytes

Used to convert between utf-8/16 strings to bytes.

Usage no npm install needed!

<script type="module">
  import utf8ToBytes from 'https://cdn.skypack.dev/utf8-to-bytes';
</script>

README

pipeline status coverage report npm version

Introduction

This is a simple npm package that is used to convert strings from utf-16, to utf-8 then, to a decimal (byte) array. You can also used it to convert from a decimal (byte) array back to utf-16.

Installation

Using npm:

npm i --save utf8-to-bytes

Using yarn:

yarn add utf8-to-bytes

Usage

To convert a string into an array.

import { stringToArray } from "utf8-to-bytes";

const arr = stringToArray("Hello World!"); // [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]

You can convert a string into an byte array.

import { stringToArray } from "utf8-to-bytes";

const arr = stringToArray("He", true); // ["01001000", "01100101"]

You convert other UTF-8 characters like emojis.

import { stringToArray } from "utf8-to-bytes";

const arr = stringToArray("🐍 With Emojis 🐳📜"); //  [ 240, 159, 144, 141, 32, 87, 105, ...],

To convert an array (byte or decimal) back into a string.

import { arrayToString } from "utf8-to-bytes";

const bytes = ["01001000", "01100101"];
const strBin = arrayToString(bytes); // "He"

const decs = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
const strDec = arrayToString(decs); // "Hello World!"

const emojis = arrayToString([240, 159]); // "🐍"

Changelog

Link to Changelog

License

Apache 2.0

Other

Credit

Logo images are a mix of the following two images: