Converting each character to a string into ASCII

bertcom
2024-11-08
2024-11-11
  • bertcom - 2024-11-08

    Good afternoon,

    I have a project where i need to split a string and send it in ascii code to an Domino Printer.

    Concept: i get an String from an Zebra scanner looking like this : " R002043;5410761402862;Oil Plus 2C Comp.A - Gris Belge;1286193824;"

    To start is the ";" the seperator. i found some functions to delete the ";" in a string. this is okay but now i want to convert each digit in the string into ascii like this:

    R=82
    0=48
    0=48
    2=50
    0=48
    ...

    This because the printer wants to receive a string like this:

    <esc>OQ001TE 82 48 48 50 48 ...<eot></eot></esc>

    Anyone who can help me with setting me on the right track with some advice?
    
     
  • TimvH

    TimvH - 2024-11-09

    Nice puzzle for a Saturday afternoon :-). Here my suggestion:

    VAR
        sInput : STRING := 'R123';
        byChar : BYTE;
        sOutput : STRING;
        i: INT;
    END_VAR
    
    sOutput := '';
    IF LEN(sInput) = 0 THEN
        RETURN;
    END_IF
    FOR i := 0 TO LEN(sInput) - 1 DO
        byChar := sInput[i];
        sOutput := Concat(sOutput, TO_STRING(byChar));
    END_FOR
    
     
  • ph0010421 - 2024-11-11

    So the printer wants an ASCII conversion of the hex equivalent of ASCII characters??
    They don't make it easy for us

     
  • bertcom - 2024-11-11

    @TimvH, Thank you, i think this way i can seperate the complete string to characters.

    Next part of the topic is converting the characters to an ASCII code. Is there an standerd function in Codesys for this?

    I alredy searched a few hours for it on the internet. no succes.

     
  • TimvH

    TimvH - 2024-11-11

    A standard string is actually a list of bytes that represent the ASCII code for each character.
    The following part of code will give you the ASCII code of one of the characters in the string:

    byChar := sInput[i];
    
     

Log in to post a comment.