Detect and remove UTF8-BOM Header

renehorstmann
renehorstmann Vreden, Germany
edited June 2 in RAPID Programming
Hey, 
I am reading a text string from a .txt file in the HOME dir.
When I edit that file on the (real) robot controller in RobotStudio, it will be encoded in UTF8-BOM and the 3 byte header: "" is added at the beginning of the file.

What would be the best way to detect and remove the BOM header out of the string I read with:

VAR iodev io;
VAR string str;
Open file,io\Read;
str:=ReadStr(io \RemoveCR\DiscardHeaders);
Close io;


my fear is that I can´t just have the string "" in the source code. Other UTF8 non ASCII glyphs (like äöü) are occasionally destroyed and replaced with other glyphs while saving/loading.

Thanks in advance
Tagged:

Best Answer

  • renehorstmann
    renehorstmann Vreden, Germany
    Answer ✓
    Fixed it,
    to not use non-ascii-glyphs in the source code, one can also use the hex representation of the BOM-Header like so:

    VAR iodev io;
    VAR string str;
    Open file,io\Read;
    str:=ReadStr(io \RemoveCR\DiscardHeaders);
    Close io;
    ! remove UTF8-BOM 3 bytes header, if available
    IF StrPart(str, 1, 3) = "\ef\bb\bf" THEN
        str := StrPart(str, 4, StrLen(str)-3);
    ENDIF

Answers

  • lemster68
    lemster68 United States ✭✭✭
    edited June 2
    Try IF str :=  "" THEN
            ! do nothing
          ENDIF

    Lee Justice
  • renehorstmann
    renehorstmann Vreden, Germany
    so are these glyphs "" ok to have in source code? "äöü" do make problems in our comments
  • lemster68
    lemster68 United States ✭✭✭
    If you were to try to view an encrypted module, you would see many strange characters, and so I could guess that the controller might be confused as to whether the module is encrypted or not with those characters present.
    Lee Justice