0

I am trying to do UTf 8 encoding to a string in node js

I have tried Buffer(someString, 'utf8') and also the package utf8 but it is always returning me the original string. var utf8 = require('utf8');

function decrypt(enc) {
  console.log('type', typeof enc);
  enc = utf8.encode(enc);
  console.log('encoding', enc);
  return enc;
}

console.log('decrypted:', decrypt('message'));

I used 'message' string in this https://mothereff.in/utf-8 and the desired result is not coming as expected. How do we do the encoding?

Aayushi
  • 1,736
  • 1
  • 26
  • 48

1 Answers1

0

Try to do it like this using the Buffer.from method, then the encoded string is converted back to a string using the buffer.tostring('utf8') method:

      function codUtf8(str) {
        const buffer = Buffer.from(str, 'utf8');
        const strCod = buffer.toString('utf8');
        return strCod;
       }

console.log(codUtf8('message')

);