How to convert an ASCII character into a hex value in TypeScript

1 Answer

0 votes
function asciiToHex(ch: string) {
    // Get the ASCII value of the character
    const asciiValue: number = ch.charCodeAt(0);
    
    // Convert the ASCII value to a hexadecimal string
    const hexValue: string = asciiValue.toString(16);
    
    return hexValue;
}

const ch: string = 'A';

const hex_value: string = asciiToHex(ch);

console.log("The hexadecimal value of '" + ch + "' is 0x" + hex_value);

   
   
/*
run:
   
"The hexadecimal value of 'A' is 0x41" 
   
*/

 



answered Dec 1, 2024 by avibootz

Related questions

1 answer 137 views
1 answer 126 views
1 answer 141 views
1 answer 135 views
2 answers 153 views
1 answer 115 views
...