Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda