Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
String.fromCharCode() not working as intended

So, I'm trying to make a simple encryption and decryption application where the user inputs are upped by some amount of characters. For Eg: user inputs "abcd" then it converts to "bcde".

Code used for encryption:

const encryptedInput = (input) => {
  try {
    // convert to array
    let splitInput = sanitize(input).split("");

    //down the characters by derived
    let mapped = splitInput.map((element) =>
      element == " "
        ? element
        : String.fromCharCode(element.charCodeAt(0) + process.env.NUMBEROFCHARS)
    );

    let encryptedInput = mapped.join(""); // result
    return encryptedInput; //return
  } catch (err) {}
};

The above code works perfectly fine. Its when I try to decrypt it back to original form, It gives me weird symbols and not the original message.

Code used for decryption:

const decryptedInput = (input) => {
  try {
    // convert to array
    let splitInput = sanitize(input).split("");

    //down the characters by derived
    let mapped = splitInput.map((element) =>
      element == " "
        ? element
        : String.fromCharCode(element.charCodeAt(0) - process.env.NUMBEROFCHARS)
    );

    let decryptedInput = mapped.join(""); // result
    return decryptedInput; //return
  } catch (err) {}
};

Output / Result:

During encryption:
Input: abcd
Output: bcde

During decryption:
Input: bcde
Output: ϕϟϩϳ

But I want the output as abcd not ϕϟϩϳ.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So I restarted my pc and the code works now.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!