Remember that a JavaScript code unit is 16 bits wide. Therefore the hex string form will be 4 digits per code unit.

usage:

var str = "\u6f22\u5b57"; // "\u6f22\u5b57" === "漢字"
alert(str.hexEncode().hexDecode());

String to hex form:

String.prototype.hexEncode = function(){
    var hex, i;

    var result = "";
    for (i=0; i<this.length; i++) {
        hex = this.charCodeAt(i).toString(16);
        result += ("000"+hex).slice(-4);
    }

    return result
}

Back again:

String.prototype.hexDecode = function(){
    var j;
    var hexes = this.match(/.{1,4}/g) || [];
    var back = "";
    for(j = 0; j<hexes.length; j++) {
        back += String.fromCharCode(parseInt(hexes[j], 16));
    }

    return back;
}
Answer from McDowell on Stack Overflow
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-convert-a-string-to-hex-on-the-server-side › m-p › 2844824
How to convert a string to hex on the server side - ServiceNow Community
February 29, 2024 - You can apply toString(16) method to each character of the string value. var inputString = "Welcome to ServiceNow"; var hexString = ""; for (var i = 0; i < inputString.length; i++) { var charCode = inputString.charCodeAt(i); // Convert character code to hexadecimal hexString += charCode.to...
🌐
GitHub
gist.github.com › shd101wyy › b49498a96715cf18b2776cc7ff8c4d95
string to hex, hex to string · GitHub
string to hex, hex to string · Raw · hex.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Liangshunet
liangshunet.com › en › 202005 › 332182170.htm
Javascript convert string to hex number and hex to string(charCodeAt, toString and parseInt)-Lionsure
May 24, 2020 - Then use toString(16) to convert Unicode value to hexadecimal, and finally use slice(-4) to intercept four digits from right. function ConvertStringToHex(str) { var arr = []; for (var i = 0; i < str.length; i++) { arr[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4); } return "\\u" + ...
🌐
Sling Academy
slingacademy.com › article › javascript-convert-string-to-hex-and-vice-versa
JavaScript: Convert String to Hex and Vice Versa - Sling Academy
June 17, 2023 - Split the input hex string into pairs of hex digits. Iterate over each pair and convert it to decimal using parseInt() with a radix of 16.
Find elsewhere
🌐
Clue Mediator
cluemediator.com › how-to-convert-a-string-to-hexadecimal-and-vice-versa-using-javascript
How to Convert a String to Hexadecimal and vice versa using JavaScript - Clue Mediator
Let's dive into the world of JavaScript hex magic! JavaScript offers a simple way to convert a string to its hexadecimal representation using the Buffer object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromHex
Uint8Array.fromHex() - JavaScript - MDN Web Docs
July 10, 2025 - The Uint8Array.fromHex() static method creates a new Uint8Array object from a hexadecimal string. This method parses the string into a byte array. To convert the string into a single number, use the parseInt() function with radix set to 16 instead.
🌐
Medium
medium.com › @saumya07013 › exploring-the-world-of-javascript-hexadecimal-operations-7501a24cc3c8
Exploring The World Of Javascript Hexadecimal Operations | by Saumya | Medium
October 1, 2024 - JavaScript provides methods to convert between decimal and hexadecimal. The toString() method allows you to convert a number to a hexadecimal string by passing 16 as the argument:
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-decimal-to-hex-in-javascript
How to convert decimal to hex in JavaScript ? | GeeksforGeeks
June 20, 2023 - Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-number-to-hex
Convert a number to Hexadecimal in JavaScript | bobbyhadz
The parseInt() function parses the supplied string argument and returns an integer of the specified radix. ... The hexadecimal numeral system uses 16 distinct symbols, so that's the radix value we should specify. You can learn more about the related topics by checking out the following tutorials: Convert Month number to Month name using JavaScript...
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › number › decimal to hexadecimal
JavaScript - Convert decimal number to hexadecimal - 30 seconds of code
September 21, 2022 - You can use parseInt() to convert a string to a number in a given base. If you don't specify a base, it will default to 10. const hexToDecimal = hex => parseInt(hex, 16); hexToDecimal('0'); // 0 hexToDecimal('ff'); // 255 ...
🌐
Medium
yasinduwishmithkorawage.medium.com › conversion-of-any-string-value-to-ascii-hex-and-vice-versa-with-javascript-872e39cd220f
Conversion of any String value to ASCII(Hex) and vice versa With JavaScript… | by Yasindu Wishmith Korawage | Medium
December 17, 2020 - The title is a problem that I had faced due to a new requirement of the client. I searched code snippets for this scenario and I couldn't find a solution according to the requirement. Therefore I exposed an algorithm for a string value to ASCII values and vice versa.
🌐
Mynikko
mynikko.com › tools › tool_jsescaper.html
MyNikko.com - Online JavaScript Hexadecimal String Escaper/Unescaper
Converts all characters to their JavaScript hexadecimal escaped sequence representation.
🌐
Code Beautify
codebeautify.org › hex-string-converter › cb42717e
js object encoded to hex back to string
Upside Down Text Letter Randomizer ... Base32 Encode Base32 Decode Base58 Encode Base58 Decode Base64 Encode Base64 Decode URL Encode URL Decode String to Hex Converter Hex to String Converter String to Binary Converter Binary to String Converter Case Converter Delimited ...