Convert into utf-8

Hello,

Coming back to n8n again after short break…!!
I wanted to create one function in CODE node…

  1. convert string into utf-8 encode.

  2. convert string into base64 encode.
    for this, i used - Buffer.from(str).toString(‘base64’);
    Is this correct?

  3. convert that string into md5.hex
    for this i used,
    var md5var = crypto.createHmac(‘MD5’, str);
    var sigHex = md5var.digest(‘hex’);
    Is this correct?

Please reply me with example.

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Shivaani_G_Soni,

  1. Convert string into utf-8 encode
    In JavaScript, strings are already UTF-8 encoded by default. However, if you need to explicitly handle UTF-8 encoding (e.g., for buffer manipulation), you can use the Buffer class.
function utf8Encode(str) {
    // Convert the string to a Buffer to get the UTF-8 bytes
    const utf8Buffer = Buffer.from(str, 'utf-8');
    
    // Convert the Buffer back to a string to visualize the bytes
    // (for demonstration purposes, showing the hex representation)
    const utf8Encoded = utf8Buffer.toString('hex');
    
    return utf8Encoded;
}

// Example usage
const str = "Hello, world!";
const utf8Encoded = utf8Encode(str);
console.log("UTF-8 Encoded (Hex):", utf8Encoded);
  1. Convert string into base64 encode
const str = "your-string-here";
const base64Encoded = Buffer.from(str).toString('base64');
console.log(base64Encoded);
  1. Convert String to MD5 Hex
    For this you’ll require to allow crypto builrin lib, can be done using below env variable.

NODE_FUNCTION_ALLOW_BUILTIN=crypto

const crypto = require('crypto');

const str = "your-string-here";
const hash = crypto.createHash('md5').update(str).digest('hex');
console.log(hash);

Would recommend using quokkajs extension in VS Code, to try this out and get to your intended functionality.

1 Like

Thank you so much for your quick reply @Roney_Dsilva…!!

It worked…!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.