Javascript string for int array

var result ="1fg";
for(i =0; i < result.length; i++){
  var chr = result.charAt(i);
  var hexval = chr.charCodeAt(chr)
  document.write(hexval + " ");
}

This gives NaN 102 103.

Perhaps because it treats "1" as an integer or something like that. Is there a way that I can convert "1" -> string to the correct integer? In this case: 49.

So it will be

49 102 103 instead of NaN 102 103

Greetings

Timo

+3
source share
1 answer

The function charCodeAttakes an index, not a string.

When you pass the string to it, it will try to convert the string to a number and use 0it if it cannot.

'1'.charCodeAt('1'). '1' . , NaN.

'f'.charCodeAt('f'). 'f' , 0, .


var hexval = result.charCodeAt(i), .

var hexval = chr.charCodeAt(0), chr.

+7

Source: https://habr.com/ru/post/1791242/


All Articles