To Calviղ s answer I ll add 2 more things.
1. We can use html entities both in decimal and hexadecimal format.
so in an html page,
a will give 'a' , its decimal and
a will also give 'a', its hexadecimal
but since you said hex, hence here s slightly modified example from Calviղ s Code
return '&#x' + c.codePointAt(0).toString(16) + ";"
2. If you also wish to include emojis in ur text or any other exotic character then charCodeAt() will not work,
its always best to use the newer codePointAt() method, unless performance is an issue.