Encoding a number string into a string of 0s and 1s in JavaScript

We need to write a JavaScript function that encodes a decimal number string into binary based on specific rules. For each digit, we determine the number of bits required, prepend (k-1) zeros followed by 1, then append the digit's binary representation.

Encoding Rules

For each digit d in the number string:

  • Let k be the number of bits needed to represent d in binary
  • Write (k-1) zeros followed by the digit 1
  • Write digit d as a binary string
  • Concatenate the results from steps 2 and 3

For example: digit 2 becomes "0110" (01 + 10), digit 3 becomes "0111" (01 + 11).

Step-by-Step Breakdown

Let's understand how digits 0-9 are encoded:

// Helper function to encode a single digit
function encodeDigit(digit) {
    const binary = digit.toString(2);
    const k = binary.length;
    const prefix = '0'.repeat(k - 1) + '1';
    return prefix + binary;
}

// Show encoding for digits 0-9
for (let i = 0; i  Binary: ${i.toString(2)} -> Encoded: ${encoded}`);
}
Digit 0 -> Binary: 0 -> Encoded: 10
Digit 1 -> Binary: 1 -> Encoded: 11
Digit 2 -> Binary: 10 -> Encoded: 0110
Digit 3 -> Binary: 11 -> Encoded: 0111
Digit 4 -> Binary: 100 -> Encoded: 001100
Digit 5 -> Binary: 101 -> Encoded: 001101
Digit 6 -> Binary: 110 -> Encoded: 001110
Digit 7 -> Binary: 111 -> Encoded: 001111
Digit 8 -> Binary: 1000 -> Encoded: 00011000
Digit 9 -> Binary: 1001 -> Encoded: 00011001

Complete Implementation

function encodeNumString(str = '') {
    let result = '';
    
    // Process each digit in the string
    for (let char of str) {
        const digit = parseInt(char);
        const binary = digit.toString(2);
        const k = binary.length;
        
        // Create prefix: (k-1) zeros + 1
        const prefix = '0'.repeat(k - 1) + '1';
        
        // Combine prefix with binary representation
        result += prefix + binary;
    }
    
    return result;
}

// Test with the example
const testString = '77338855';
const encoded = encodeNumString(testString);
console.log(`Input: ${testString}`);
console.log(`Encoded: ${encoded}`);
Input: 77338855
Encoded: 001111001111011101110001100000011000001101001101

Decoding Function

Here's how to reverse the encoding process:

function decodeNumString(encodedStr) {
    // Create lookup table for encoded digits
    const encodingMap = {};
    for (let i = 0; i  0) {
        let found = false;
        
        // Try to match each possible encoding
        for (let encoded in encodingMap) {
            if (remaining.startsWith(encoded)) {
                result += encodingMap[encoded];
                remaining = remaining.slice(encoded.length);
                found = true;
                break;
            }
        }
        
        if (!found) break;
    }
    
    return result;
}

// Test decoding
const encoded = '001111001111011101110001100000011000001101001101';
const decoded = decodeNumString(encoded);
console.log(`Encoded: ${encoded}`);
console.log(`Decoded: ${decoded}`);
Encoded: 001111001111011101110001100000011000001101001101
Decoded: 77338855

Key Points

  • Each digit requires different encoding lengths based on its binary representation
  • Digits 0-1 use 2 bits, 2-3 use 4 bits, 4-7 use 6 bits, 8-9 use 8 bits
  • The prefix pattern (k-1 zeros + 1) helps identify where each encoded digit begins
  • This encoding is reversible and preserves the original digit sequence

Conclusion

This encoding scheme creates a unique binary representation for decimal strings by prefixing each digit's binary form with a length indicator. The method ensures unambiguous decoding while maintaining the original digit order.

Updated on: 2026-03-15T23:19:00+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements