How to convert an ASCII character into a hex value in Node.js

1 Answer

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

const ch = 'z';

const hex_value = asciiToHex(ch);

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

   
   
/*
run:
   
The hexadecimal value of 'z' is 0x7a
   
*/

 



answered Dec 1, 2024 by avibootz
edited Dec 1, 2024 by avibootz

Related questions

1 answer 131 views
1 answer 119 views
1 answer 135 views
1 answer 129 views
2 answers 145 views
...