Convert String to Bytes using UTF-8

Surbhi Srivasta
Tera Expert

Hi,

I am stuck at an issue where I need to convert a String into Bytes using UTF-8 format to be sent to a 3rd party application. 3rd party application only support parameters in this format.

Any pointers on how we can do this. 

 

Regards,

Surbhi

7 REPLIES 7

Samaksh Wani
Giga Sage
Giga Sage

Hello @Surbhi Srivasta 

 

 

function toUTF8Array(str) {
    var utf8 = [];
    for (var i=0; i < str.length; i++) {
        var charcode = str.charCodeAt(i);
        if (charcode < 0x80) utf8.push(charcode);
        else if (charcode < 0x800) {
            utf8.push(0xc0 | (charcode >> 6), 
                      0x80 | (charcode & 0x3f));
        }
        else if (charcode < 0xd800 || charcode >= 0xe000) {
            utf8.push(0xe0 | (charcode >> 12), 
                      0x80 | ((charcode>>6) & 0x3f), 
                      0x80 | (charcode & 0x3f));
        }
        else {
            // let's keep things simple and only handle chars up to U+FFFF...
            utf8.push(0xef, 0xbf, 0xbd); // U+FFFE "replacement character"
        }
    }
    return utf8;
}

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

Hi @Samaksh Wani 

Thank you so much for your response. I am not very familiar with this type of conversion and hence cross checking.

Solution provided above gives me output as Comma Separated Numbers. Does this mean the string which I pass in the function gets converted into Bytes?

Sample value shared by 3rd party team is as below and they are expecting in this format which they can support.

Can you please help me on how to convert the string into below format?

789ea41b-6d90-363a-80d7-b27c9af42a12

 

Regards,

Surbhi

Hello @Surbhi Srivasta 

 

I dont think you are looking for UTF-8 Byte Conversion,

 

If you are looking for String encryption :-

 

 

var enc = new GlideEncrypter();
var myPassword= "HelloWorld";
var encypted_myPassword = enc.encrypt(myPassword);
gs.info(encypted_myPassword);

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

-O-
Kilo Patron
Kilo Patron

Do you meant to say that the body needs to be a multi-part binary content?

In that case the only possibility is to use the Flow Designer REST step selecting POST as HTTP method and Binary as Request type:
2023-07-16-1.jpg

The "source" would be an attachment.