Home > Net >  Check if no unicode character is associated with a number Javascript
Check if no unicode character is associated with a number Javascript

Time:07-02

I am making a base converter that can convert any number in any base to the same number in any other base. Since the bases could be any number the result could have digits that represent a number greater than 10. In order to have only one character correspond to each digit in the final result, I plan to assign Unicode characters to each digit corresponding to a value greater than 10 by using String.fromCharCode(Base 10 number digit represents). However, I noticed that there was a problem with this method; It seems as if a lot of the values are blank when outputted onto the HTML webpage or only output an empty box that looks like this: . When I tried to see if the value of the Unicode character equaled the empty box or an empty string by using an if statement, it always seemed to return false, even when the Unicode character associated with the digit returned the same empty box. Please may you help me understand why this is, and how I can construct the if statement such that it will work like I want it to?

CodePudding user response:

You could try this:

function convertToDecimal(num, base) {
    return window.parseInt(num.toString(), base);
}

const binary = 10;
const base = 2;

console.log("Decimal version:", convertToDecimal(binary, base))
  • Related