javascript-binary-converter is a simple utility to convert various binary data objects in Javascript, like Blob, File, TypedArray and others. It also provides conversion abilities from various notations(binary,hex,decimal,"bytes") to others.
Works both in the browser and in Node.js(with limitations).
If you encounter any bugs or have a question, please don't hesitate to open an issue.
:warning: The API is still unstable and might undergo significant changes. Do not rely on semver, until it reaches version 1.0
Installation
$ npm install javascript-binary-converter
Usage in an ES6 modules environment:
import converter from "javascript-binary-converter";
Usage in Node.js(CommonJS):
const converter = require("javascript-binary-converter").default;
Usage in the browser, in a classical HTML page:
You will need to use the umd file. Note that auto complete wont work like this(every call to converter will result in "any" in your IDE). First import the program from the umd build:
<script src="/node_modules/javascript-binary-converter/build/umd/index.js"></script>
This exposes the program on the window object:
const javascriptBinaryConverter = window['javascript-binary-converter']
const {converter} = javascriptBinaryConverter;
Table of Contents
Concept
converter is a function that accepts any of the convertible objects, recognize the type, and expose the conversion methods that exist for this specific object.
Just pass the object you want to convert, and call the appropriate method.
Usage in Node
Not all conversion functionality is available in Node, due to natural limitations. For instance, Image conversion is unavailable, because this object
simply doesn't exist in Node(same goes for the File object). The program will throw an exception in these cases.
If, for some reason, you need to convert a Blob object(available in Node since version 15) to some other type, import BlobConverter directly, instead of using the generic converter function, which will show incorrect method signatures in this case(some internal limitations).
Examples
File to image
Convert a File(browser object) to an Image.
converter function recognizes the type passed to it, exposing the relevant methods. In this case a File object is passed,
invoking FileConvertor behind the scenes:
import converter from "javascript-binary-converter";
document
.querySelector("#some-file-input")
.addEventListener("change", async (event) => {
const target = event.target;
const files = target.files;
const image = await converter(files[0]).toImage({ maxSize: 200 });
document.querySelector("#image-preview").appendChild(image);
});
File to bytes
If you want to get raw binary data of a File:
document
.querySelector("#some-file-input")
.addEventListener("change", async (event) => {
const target = event.target;
const files = target.files;
const bytes = await converter(files[0]).toBytes();
});
Blob to Image
import converter from 'javascript-binary-converter';
(async () => {
const blob = new Blob([...])
const image = await converter(blob).toImage({type:"image/png"})
})();
Image to Uint8Array
const image = document.querySelector("#some-image");
const uint8 = await converter(image).toUnit8Array();
Image to byte decimals
"byte decimals" are normal bytes, just in decimal notation instead of binary.
const image = document.querySelector("#some-image");
const byteIntegers = await converter(image).toByteIntegers();
Uint8Array to Image
Suppose you have an array of integers, each representing a byte, in an unsigned 8 bit integer. You can convert those bytes into an html image(assuming the bytes represent an actual image):
const dummyImageByteIntegers = [
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 5, 0, 0,
0, 6, 8, 6, 0, 0, 0, 11, 251, 84, 75, 0, 0, 0, 1, 115, 82, 71, 66, 0, 174, 206,
28, 233, 0, 0, 0, 137, 73, 68, 65, 84, 24, 87, 1, 126, 0, 129, 255, 1, 255, 106,
48, 255, 213, 212, 208, 0, 0, 254, 0, 0, 43, 47, 29, 0, 0, 14, 18, 0, 1, 255, 113,
56, 255, 219, 204, 200, 0, 37, 43, 31, 0, 249, 245, 242, 0, 1, 8, 13, 0, 1, 251,
79, 26, 255, 217, 218, 230, 0, 43, 68, 39, 0, 220, 198, 217, 0, 36, 40, 26, 0,
1, 255, 85, 39, 255, 198, 199, 217, 0, 48, 52, 16, 0, 240, 241, 240, 0, 250, 247,
1, 0, 1, 249, 94, 58, 255, 183, 187, 198, 0, 37, 45, 16, 0, 37, 36, 36, 0, 217,
208, 221, 0, 1, 224, 77, 47, 255, 11, 16, 9, 0, 20, 66, 54, 0, 0, 235, 236, 0,
226, 197, 211, 0, 66, 133, 49, 43, 23, 66, 67, 118, 0, 0, 0, 0, 73, 69, 78, 68,
174, 66, 96, 130,
];
const uint8 = new Uint8Array(dummyImageByteIntegers);
const image = await converter(uint8).toImage();
document.body.appendChild(image);
Converting raw bytes
"raw bytes", in this regard, is either an array of "bits"(a string of zeros and ones), or of decimal numbers, representing a byte(0 to 255 if unsigned, -128 to 127 if signed). Do not pass binary digits of 1 and 0, as numbers(like 10111101). If you need this, use a string("10111101"). Please note that this functionality might still have some logical flaws, so feel free to open an issue if you suspect you found one. Also, efficiency might be a problem, with very large data.
Byte decimals to Int16Array
:warning: Important: Being that int16 uses two bytes(16 bits) to represent 1 number, the program will "group" the bytes into groups of 2.
In the following case, 4 bytes will result in 2 numbers. Note that being that Int16 is signed, any decimal you pass will result in the two's complement binary.
:warning: About byte order: Any conversion to integers larger than 8, will rely on the system's byte order, which is usually little-endian(which might be counter intuitive).
const bytes = [-127,-127, -1, 2]
const int16 = converter(bytes).toInt16Array()
Bytes to decimals
As mentioned above, you can also pass real "bytes", in the form of a string. Each string needs to be 8 character long.
const bytes = ['11111111', '11111111', '11110111']
const decimals = converter(bytes).toIntegers({isSigned:true})
Bytes to hex
const bytes = ['11010100', '11111100','10001000']
const hexes = converter(bytes).toHex({isSigned:false})
Converting decimals to other notations
Integer to binary
const decimal = 4434
const binary = converter(decimal).toBinary()
Float to binary
const binary = converter(-0.32323).toBinary()
Integer to hex
const hex = converter(17868022686844715136n).toHex()
Float to hex
const hex = converter(-0.43431).toHex()
Integer to decimal bytes
const bytes = converter(422).toDecimalBytes();
Converting hex to other notations
Hex to integer
const integer = converter('FFF4').toInteger()
Signed Hex to integer
you can also assert that the hex is of two's complement convention
const integer = converter('FFF4').toInteger({isSigned:true})
Hex to binary
const binary = converter('296C167').toBinary()
Hex to float
const float = converter('bede61d0').toFloat()