As a self-contained solution in functional style, you can encode with:

plain.split("")
     .map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
     .join("");

The split on an empty string produces an array with one character (or rather, one UTF-16 codepoint) in each element. Then we can map each to a HEX string of the character code.

Then to decode:

hex.split(/(\w\w)/g)
   .filter(p => !!p)
   .map(c => String.fromCharCode(parseInt(c, 16)))
   .join("")

This time the regex passed to split captures groups of two characters, but this form of split will intersperse them with empty strings (the stuff "between" the captured groups, which is nothing!). So filter is used to remove the empty strings. Then map decodes each character.

Answer from Daniel Earwicker on Stack Overflow
🌐
GitHub
gist.github.com › valentinkostadinov › 5875467
JavaScript HEX encoding · GitHub
JS has atob & btoa but hex processing can only be done on Numbers in vanilla JS. Edit: atob and btoa aren't part of ES, so they don't count ... > h = 'hello 🤣'; 'hello 🤣' > const encoded = Buffer.from(h).toString('hex'); undefined > encoded '68656c6c6f20f09fa4a3' > Buffer.from(encoded, 'hex').toString(); 'hello 🤣' >
🌐
GitHub
github.com › fabiospampinato › hex-encoding
GitHub - fabiospampinato/hex-encoding: Hex encoding. An extremely fast and synchronous JS implementation. · GitHub
import Hex from 'hex-encoding'; // Hex encoding & decoding { const raw = 'Hello 😃'; const uint8 = new TextEncoder ().encode ( raw ); console.log ( uint8 ); // => Uint8Array(10) [ 72, 101, 108, 108, 111, 32, 240, 159, 152, 131 ] const encoded = Hex.encode ( uint8 ); console.log ( encoded ); // => '48656c6c6f20f09f9883' const decoded = Hex.decode ( encoded ); console.log ( decoded ); // => // => Uint8Array(10) [ 72, 101, 108, 108, 111, 32, 240, 159, 152, 131 ] } // String encoding & decoding { const raw = 'Hello 😃'; const encoded = Hex.encodeStr ( raw ); console.log ( encoded ); // => '48656c6c6f20f09f9883' const decoded = Hex.decodeStr ( encoded ); console.log ( decoded ); // => 'Hello 😃' } // Check if a string is hex-encoded { console.log ( Hex.is ( '48656c6c6f20f09f9883' ) ); // => true console.log ( Hex.is ( '😃' ) ); // => false }
Author: fabiospampinato
🌐
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.
🌐
PortSwigger
portswigger.net › bappstore › 1da8a127327d42eabff3725efe93db86
JavaScript Encode As - PortSwigger
January 31, 2025 - This extension allows you to encode strings in the JavaScript format (i.e. Unicode/Hex), similar to Burp's Convert-To but specifically for within JSON/JavaScript.
🌐
DEV Community
dev.to › catur › simple-encode-and-decode-with-javascript-od6
Simple Encode And Decode With Javascript - DEV Community
February 14, 2023 - the method above is how to do encoding and decoding using javascript. Using base64, the results that appear may contain special characters, such as the "=" character. If we want the result without using special characters, we can use another method, namely with hex.
🌐
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" + arr.join("\\u"); } ... Mainly achieved through the replace() and unescape() methods. First replace all \ in the string with % using replace(), then use unescape() to encode all characters (Unicode characters) in %uxxxx format with hexadecimal Unicode characters in xxxx format instead...
Find elsewhere
🌐
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 - var originalString = "Hello, World!"; // Convert string to hexadecimal var hexString = ''; for (var i = 0; i < originalString.length; i++) { hexString += originalString.charCodeAt(i).toString(16); } // Output hexadecimal representation gs.info("Hexadecimal representation: " + hexString);
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-string-exercise-27.php
JavaScript validation with regular expression: Convert ASCII to Hexadecimal format - w3resource
July 22, 2025 - Write a JavaScript function that converts an ASCII string into its hexadecimal representation character by character.
🌐
npm
npmjs.com › search
hex-encode-decode - npm search
Convert character encodings in pure javascript.
🌐
Code Beautify
codebeautify.org › hex-string-converter › cb42717e
js object encoded to hex back to string
Hex to String converter is easy to use tool to convert Hex data to plain text.
🌐
SSOJet
ssojet.com › binary-encoding-decoding › base16-hexadecimal-in-javascript-in-browser
Base16 (Hexadecimal) in JavaScript in Browser | Binary to Text Encodings in Programming Languages
In modern browsers, the TextEncoder and TextDecoder APIs provide a robust way to handle this. The TextEncoder API converts a JavaScript string into a Uint8Array using UTF-8 encoding, which can then be transformed into a hexadecimal string.
🌐
npm
npmjs.com › package › hex-encode-decode
hex-encode-decode - npm
February 26, 2019 - Hex encode & decode string · Install · Usage · Contribute · License · This project uses node and npm. $ npm install hex-encode-decode · $ # OR · $ yarn add hex-encode-decode · const { encode, decode } = require('./') console.log(encode('hello'), decode('68656c6c6f')) // 68656c6c6f hello ·
      » npm install hex-encode-decode
    
Published: Feb 26, 2019
Version: 1.0.0
🌐
Medium
medium.com › deno-the-complete-reference › deno-nuggets-encode-decode-hex-string-ca990ce60217
Encode/decode hex string in Deno | Tech Tonic
April 6, 2023 - const te=(s:string)=>new ... To create a hex string, the hex encode function need to be used to convert a text encoded string into hex data that would be text decoded to get a hex string....