Hy everybody, first post here so please have mercy π.
I'll go straight to the question:
I read from a file a string that represents with hex values a series of chars in a string
examples: '41' => 'A'; '535458' => 'STX'.
My concern is about robustness and code readability, i do not need great efficiency since the script will run only once during the initialization phase of the system and so does not need to be fast.
I have the feeling that I am missing something very obvious.
Can you please point me thowards the right direction? I looked inside the OSCAT library but could not find something satisfying.
Thank you for your attention and happy holidays.
Last edit: Iaxexo 2021-12-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I do not believe there is an available function that handles this conversion for you, so try this out. This handles partial hex (e.g. 20F) and also padded (020F).
Notes:
1. Make sure to fill out the _asChar array with all of the chars from the ascii table (e.g. https://www.asciitable.com/). I just tested the first few chars, but the ones of interest should start at 48 (0) and ends at 122 (z), but feel free to adjust further (e.g. from 32 (space) to 126 (~)). If you have other methods that could also use the _asChar, then probably should move it to a global variable to be reused.
2. Install the Utils library, which has the HEXinASCII_TO_BYTE method.
FUNCTION sHexToAscii : STRING;VAR_INPUT sInput : STRING;END_VARVAR _bTemp : BYTE; // get value _uiIndex : UINT; // loop index _asChar : ARRAY[48..122] OF STRING(1) := ['0','1','2','3','4','5','6','7','8','9','0']; // fill _uiStrLen : UINT; // input string length _uiStart : UINT := 0; // loop start indexEND_VAR
Util library has the following:
BYTE_TO_HEXinASCII (FUN) (byte to corresponding word)
HEXinASCII_TO_BYTE (FUN) (corresponding word to byte)
WORD_AS_STRING (FUN) (corresponding word to string)
Hy everybody, first post here so please have mercy π.
I'll go straight to the question:
I read from a file a string that represents with hex values a series of chars in a string
examples: '41' => 'A'; '535458' => 'STX'.
I have found the ugliest possible working code:
My concern is about robustness and code readability, i do not need great efficiency since the script will run only once during the initialization phase of the system and so does not need to be fast.
I have the feeling that I am missing something very obvious.
Can you please point me thowards the right direction? I looked inside the OSCAT library but could not find something satisfying.
Thank you for your attention and happy holidays.
Last edit: Iaxexo 2021-12-23
what exactly is your question?
Sorry: Is there a better way to do such conversion?
Are there any functions or librarys that could shorten the code?
@laxexo,
I do not believe there is an available function that handles this conversion for you, so try this out. This handles partial hex (e.g. 20F) and also padded (020F).
Notes:
1. Make sure to fill out the
_asChar
array with all of the chars from the ascii table (e.g. https://www.asciitable.com/). I just tested the first few chars, but the ones of interest should start at 48 (0) and ends at 122 (z), but feel free to adjust further (e.g. from 32 (space) to 126 (~)). If you have other methods that could also use the _asChar, then probably should move it to a global variable to be reused.2. Install the Utils library, which has the
HEXinASCII_TO_BYTE
method.Nice solution, thank you! I hoped for something "easier" but I am beginning to understand that this is not a happy place for string manipulations :)
Last edit: Iaxexo 2022-01-11
Util library has the following:
BYTE_TO_HEXinASCII (FUN) (byte to corresponding word)
HEXinASCII_TO_BYTE (FUN) (corresponding word to byte)
WORD_AS_STRING (FUN) (corresponding word to string)
https://product-help.schneider-electric.com/Machine%20Expert/V1.1/en/Util/index.htm?#t=topics%2Ffld-hexascii-functions.htm