Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Javascript - Replace all emoji in a string to unicode and back

Let's say I have a string as such:

var text = "Hi ๐Ÿ˜‚๐Ÿ˜‚";

I want to store this text in a database and wish to convert the emoji's to unicode.

My current solution is to convert the string to an array. Then convert each character to unicode.

 function toUnicode(text){
    var arr = Array.from(text);
    var unicodeArr = [];
    for (let i = 0; i < arr.length; i++) {
        var unicode = arr[i].codePointAt(0).toString(16);
        unicodeArr.push(unicode);
    }
    return JSON.stringify(unicodeArr);
}

Here are the output when I run the code:

var text = "Hi ๐Ÿ˜‚๐Ÿ˜‚";

var unicodeArr = toUnicode(text);
console.log(unicodeArr); //["48","69","20","1f602","1f602"]

The only issue is that I'm not sure how to convert this back to a whole string.

Is there a much more easy solution to store this var text = "Hi ๐Ÿ˜‚๐Ÿ˜‚"; in a Database?

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

You can convert to base64 which is platform agnostic:

var text = "Hi ๐Ÿ˜‚๐Ÿ˜‚";

const b64 = btoa(unescape(encodeURIComponent(text)));
const txt = decodeURIComponent(escape(atob(b64)));

console.log(b64);
console.log(txt);

about 4 years ago ยท Juan Pablo Isaza Report

0

I modified your "toUnicode" function to return an array of numbers and not a jsonified array of number.toString, so I didn't have to parse it all again to unpack it. Give this a try:

function toUnicode(text){
  return Array.from(text).map(char => char.codePointAt(0));  
}

function fromUnicode(unicode) {
  return unicode.map(num => String.fromCodePoint(num)).join("");
}

var text = "Hi ๐Ÿ˜‚๐Ÿ˜‚";
var unicodeArr = toUnicode(text);
var backToString = fromUnicode(unicodeArr);
console.log(text, unicodeArr, backToString);

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!