Encode Alphabet to Numbers with Alphanumerics.js

Category: Javascript | January 9, 2025
Author:MFM-347
Views Total:0 views
Official Page:Go to website
Last Update:January 9, 2025
License:MIT

Preview:

Encode Alphabet to Numbers with Alphanumerics.js

Description:

Alphanumerics is a lightweight JavaScript utility that enables encoding and decoding of text into numbers.

The library offers two simple functions to achieve this. The encode function transforms regular text into a numeric representation. Conversely, the decode function converts this numeric representation back to its original form.

Use Cases:

  • Data Obfuscation: Protect sensitive information by encoding it into a numeric format.
  • Educational Tools: Teach students about alphabetic positions and basic encoding techniques.
  • Simple Encryption: Use it for basic encryption needs in applications.

How to use it:

1. Install Alphanumerics with NPM:

# NPM
$ npm install alphanumerics

2. Convert a string into its alphabetic numeric representation.

import { encode } from 'alphanumerics';
const encodedText = encode('CSS Script Com');
// => 3-19-19 19-3-18-9-16-20 3-15-13
console.log(encodedText);

3. Convert an encoded numeric string back into the original text.

import { decode } from 'alphanumerics';
const decodedText = decode('3-19-19 19-3-18-9-16-20 3-15-13');
// => CSS Script Com
console.log(decodedText);

How It Works

The library functions through direct string manipulation and character code conversion.

The encode function first checks if the input is valid text. If it is, the function splits the input string into words. Each word is then further split into individual letters. For each letter, its ASCII code is obtained. If the character code corresponds to a lowercase letter (a-z), 96 is subtracted from the code to get its alphabetical position (a=1, b=2, etc.). These numeric positions are joined by hyphens. Finally, the numeric representations of the words are joined by spaces.

The decode function also starts by validating the input. It splits the encoded string by spaces to separate the encoded words. Each encoded word is then split by hyphens to get the individual numeric letter codes. For each numeric code, 96 is added. This resulting number is then converted back into its corresponding character using String.fromCharCode(). The decoded letters are joined to form words, and the decoded words are joined by spaces to produce the final output.

You Might Be Interested In:


Leave a Reply