Converting Strings to integers!

July 06, 2021

Strings and integers are two different types of data structures. But how to convert one into the other?

Integers to Strings

Converting integers to strings are fairly simple. You just need to add an empty string to the integer.

var num = 9;
num += ""; // now, num is a string containing "9"

Strings to integers

Strings cannot be converted back to integers by the same method. This is because if you add a string with a number, the result will be another string. Thus, if you add a number to a string, that number will be appended to that string, instead of being mathematically added. This is because of type coercion:

var str = "90";
str += 0; // instead of str storing an integer, str will now store "900"

parseInt(): the easy way out

However, there is a simple alternative to get around this problem: the function parseInt(). This function converts a string to an integer and returns that integer. But, if there are non-integer characters in the string that is passed, the parseInt() function, evaluates it from left-to-right. Thus,

parseInt("7"); // output: 7
parseInt("0007"); // output: 7
parseInt("7am"); // output: 7
parseInt("March7"); // output: NaN

Thus, using parseInt(), it is fairly easy to convert a string into an integer. Not only this, the function also allows you to read the contents of a string as an integer of a specific mathematical system. So, along with the string itself, parseInt() lets you pass a second argument called ‘radix’. This tells the function the ‘positional numerical system’ that the number (contained in the string) belongs to. So, if the string "100" is passed, normally, the return would be 100, but if it is passed along with 2, it will return 4:

parseInt("100"); //output: 100 
parseInt("100", 2);//output: 4 [since 100 in binary means 4]
parseInt("100", 10);//output: 100 [since 10 in radix means decimal numerical system]
parseInt("7", 2);//output: NaN, since 7 is not a part of the binary system.

Writing the parseInt() function

Life with parseInt() is simple and easy. But it is, after all, a function. So, by definition, the world was not created so simple and easy. There must be a specific line of codes that is extracting the contents of a string and returning its numerical equivalent? What if I try to a write a simpler version of that same function?

To be honest, this was not an easy task. But after some toil, I figured that the function I was attempting to write should do one key task: extract the number in the units position and multiply it with 1, and then extract the number in the tenth position and multiply it by 10 and so on. Thus, the function should work somewhat like this:

f("123") = 1 * 100 + 2 * 10 + 3 = 123

Using this logic, I wrote the following code:

function parse(str){
   
        switch(str){
            case "0":
                return 0;
            case "1":
                return 1;
            case "2":
                return 2;
            case "3":
                return 3;
            case "4":
                return 4;
            case "5":
                return 5;
            case "6":
                return 6;
            case "7":
                return 7;
            case "8":
                return 8;
            case "9":
                return 9;

        } 
    return parse(str.substring(0, str.length - 1)) * 10 + parse(str[str.length - 1]);
}
console.log(parse("123") + 1);

Using Unicode values instead

I realised that there was another, simpler way, of writing the base case. That is, there is a way to write the base case without having to write a switch-case for every digit. This can be done by converting the string passed to the base case to its unicode character. To do this, two methods are essential. The method charCodeAt() returns an integer representing the unicode value of the character in a specific index of a string. Conversely, the method String.fromCharCode() returns a string containing a sequence of characters, by converting the unicode units. This is how it works:

"9".charCodeAt(0); // output: 57, which is the unicode value of '9'
"waffles".charCodeAt(2); // output: 102, which is the unicode value of 'f'
String.fromCharCode(57); // output: '9'
String.fromCharCode(102); //output: 'f'

What is important to note, for the problem at hand, is that, the unicode values will always be sequential. That is to say, the unicode values of '0' to '9' will always come one after the above (namely, 48 to 57). Same goes for 'a' to 'z' (97 to 122) and 'A' to 'Z' (65 to 90) and so on.

Thus, using the above methods, the base case of the above program can be re-written in the following manner:

function parse(str){
   if(str.length == 1){
       return str.charCodeAt(0) - "0".charCodeAt(0);
   }
    return parse(str.substring(0, str.length - 1)) * 10 + parse(str[str.length - 1]);
}
console.log(parse("123") + 1);