How to convert a string to ASCII

harishdasari
Tera Guru

Hello,

We have requirement to convert string to ASCII values.

for example: "hello " is the string which needs to be converted to ASCII value 72,69,76,76,79

Thank you.

1 ACCEPTED SOLUTION

Vishal Khandve
Kilo Sage
var string = "Some string";

for (var i = 0; i < string.length; i++) {
  console.log(string.charCodeAt(i));
}

View solution in original post

7 REPLIES 7

harishdasari
Tera Guru

Hi Vishal and Sunil,

how can we exclude the spaces  and dash(-) from the string, it is calculating ASCII for spaces and dash as well.

could you please help me with that.

Thanks

str = str.replace(/\s/g, ""); //removes all spaces

Thank you Sunil. it worked.