How to convert string to hex in TypeScript

1 Answer

0 votes
function string_to_hex(str: string) {
    let arr: string[] = [];
      
    for (let i: number = 0, len = str.length; i < len; i ++) {
        const hex = Number(str.charCodeAt(i)).toString(16);
        arr.push(hex + ' ');
    }
      
    return arr.join('');
}
  
  
console.log(string_to_hex("typescript")); 

    
      
      
/*
run:
      
"74 79 70 65 73 63 72 69 70 74 " 
      
*/

 



answered Nov 18, 2024 by avibootz

Related questions

1 answer 240 views
1 answer 185 views
2 answers 508 views
4 answers 580 views
...