utf8-to-bytes
v0.1.3
Published
Used to convert between utf-8/16 strings to bytes.
Downloads
8
Maintainers
Readme
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
License
Other
- Inspired by convert-string
Credit
Logo images are a mix of the following two images: