gunescelil
gunescelil

Reputation: 313

String concatenation is not happening in JS SpiderMonkey

I have a deployment environment where I am running my JS codes in SpiderMonkey JS engine.

I had to do some conversion from HEX to ASCII. I am iterating through a HEX string and concatenating to a string variable. This code can work in Nashorn Engine but it could not work in SpiderMonkey

function hextoascii(str1)
{
    var hex  = str1.toString();
    var str = '';
    var substr = '';
    var parsedInt = 0;
    for (var n = 0; n < hex.length; n += 2) 
    {
        substr = hex.substr(n, 2);
        parsedInt = parseInt(substr, 16);
        str = '' + str.concat(String.fromCharCode(parsedInt));          
        log.info("n: " + n + " - parsedInt: " + parsedInt + " str: " + str);
    }
    return str;
}

The output is as follows:

n: 0 - parsedInt: 77 str: M
n: 2 - parsedInt: 90 str: MZ
n: 4 - parsedInt: 144 str: MZ
n: 6 - parsedInt: 0 str: MZ
n: 8 - parsedInt: 3 str: MZ

How to fix my concatenation?

Upvotes: 0

Views: 47

Answers (0)

Related Questions