Skip to Main Content
Centre d’assistance Cognex
LogoLogo

How to Replace Control Characters in a Result String with Any Character

How to Replace Control Characters in a Result String with Any Character.

18/04/2025

Question

How to replace control characters which are included in the read result with any character.

Answer

Using Scripting in Format Data allows you replacing the control characters which are included in the result string with any character.

For this sample, replacing control character 0x1d with pipe (vertical bar).

Reset Configuration of the device and enable Script-Based Formatting, then copy and paste the below code into the Data Formatting Script, overwriting any existing code there.

Placeholder
placeholder

 

function onResult (decodeResults, readerProperties, output)

{

             if (decodeResults[0].decoded)

             {

                           output.content = "";

                           for(var i=0; i<decodeResults[0].content.length; i++)

                           {

                           if(decodeResults[0].content[i].charCodeAt().toString(16) == "1d")

                                                       output.content += "|";

                                         else      

                                                       output.content += decodeResults[0].content[i];

                           }

                           output.content += "\r\n";

             }

}

Loading component...

Ressources connexes

Loading component...