@manalejandro/text2bin
v1.0.2
Published
Simple utility to convert text to binary
Maintainers
Readme
@manalejandro/text2bin
A simple and lightweight utility to convert text to binary representation.
Installation
npm install @manalejandro/text2binUsage
ES6 Modules
import { text2bin } from '@manalejandro/text2bin';
const binaryText = text2bin('Hello');
console.log(binaryText); // 0100100001100101011011000110110001101111CommonJS (if your project doesn't use ES modules)
Since this package uses ES modules, you'll need to import it using dynamic imports in CommonJS:
const { text2bin } = await import('@manalejandro/text2bin');
const binaryText = text2bin('Hello');
console.log(binaryText); // 0100100001100101011011000110110001101111API
text2bin(text)
Converts a string to its binary representation.
Parameters
text(string): The text to convert to binary
Returns
- (string): The binary representation of the input text, where each character is converted to an 8-bit binary string
Example
import { text2bin } from '@manalejandro/text2bin';
// Basic usage
console.log(text2bin('A')); // 01000001
console.log(text2bin('Hello')); // 0100100001100101011011000110110001101111
console.log(text2bin('123')); // 001100010011001000110011
// Empty string
console.log(text2bin('')); // (empty string)
// Special characters
console.log(text2bin('!@#')); // 001000010100000000100011How it works
The function:
- Splits the input text into individual characters
- Converts each character to its ASCII/Unicode code point using
charCodeAt(0) - Converts the code point to binary using
toString(2) - Pads each binary representation to 8 bits using
padStart(8, '0') - Joins all binary representations into a single string
Features
- ✨ Simple and lightweight
- 🚀 Fast conversion
- 📦 Zero dependencies
- 🔧 ES6 module support
- 🛡️ Safe handling of empty/null inputs
License
MIT
Author
ale
Keywords
- text2bin
- text to binary
- binary conversion
- text encoding
- binary encoding
Contributing
Feel free to submit issues and enhancement requests!
