Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

How to convert a HEX value to a char?

Fri May 06, 2016 9:18 am

I'm trying to fill an associative array with mappings from characters to their HEX encoding.

The below fails: it doesn't like the `("\0".$hexChar)` construct.

So: how do I convert a string containing the characters "\", "0" and "1" (where "0" and "1" are dynamically generated) so it parses as "\01"?
:global convert ({});
#:set ($convert->"\00") "\\00"
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:local hexChars "01234567890ABCDEF"
:local groupChars "01890ABCDEF"
  :for hexCharsIndex from=0 to=([:len $hexChars] - 1) do={ 
    :local hexChar [:pick $hexChars $hexCharsIndex]
    :set ($convert->("\0".$hexChar)) ("\\0".$hexChar)
  }
--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to convert a HEX value to a char?

Fri May 06, 2016 2:04 pm

Doing a single slash means the next character or two are expected to form a valid escape sequence. "\0" on its own is not a valid escape sequence, unless you have a literal second hex character there, e.g. "\0F".

To put this another way, on a parser level,
"\0".$hexChar
is three main tokens: a string, a concatenation operator, and a variable access operator. Within a string, each character other than a slash is valid, but a slash must either be followed by one of the second characters from the constant escape sequences, or the next two characters must form a valid hex number.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: How to convert a HEX value to a char?

Fri May 06, 2016 4:49 pm

...To put this another way, on a parser level,
"\0".$hexChar
is three main tokens: a string, a concatenation operator, and a variable access operator.
That I already figured. Hence the above question: How can I do this despite the parser?
Is there a kind of `eval` or `parse` function that I could use? And if so: where should I start?

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to convert a HEX value to a char?

Fri May 06, 2016 8:34 pm

Yes. There's the :parse command. It takes a string, and returns a callable command. If you store it in a variable, then referencing the variable will trigger the command.
(that one's been in use from before :local and :global could declare functions with the "do" argument)

There's also :execute, which is similar, but instead of returning the command, is executes it right away. It's "the" equivalent of eval().
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: How to convert a HEX value to a char?

Fri May 06, 2016 10:04 pm

Thanks. Any good examples to get started (before I find myself in quoting hell).
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: How to convert a HEX value to a char?

Sat May 07, 2016 7:46 am

Thanks. Any good examples to get started (before I find myself in quoting hell).
Google Search isn't exactly helpful, as these hardly return anything:
And I know from experience in other languages that these kind of constructs are really tricky.

Another question: how bad is the performance of `:parse` and `:execute` compared to normal script execution?

It's about replacing ~150 lines of initialisation code with a double loop.

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to convert a HEX value to a char?

Sat May 07, 2016 5:04 pm

Obviously, it's slower than executing the actual code directly, but AFAIK, it's not THAT much slower (I haven't done any measurements, but there's certainly not a noticeable difference). The penalty adds up if you call :parse/:execute multiple times, but if you compose everything, before eventually calling it once, there shouldn't be much different, compared to executing the generation, and then executing "real code equivalent".

The larger parts of your performance concerns should be at the actual generation, as there's no "append" operation for strings. Every concatenation produces a new value, and then you overwrite the old value, and you do that each time you want to append to a string, which is not exactly very efficient, and adds up on large loops. I don't think 150 overwrites add THAT much time, but still, it would be noticeably slower if you were comparing 1000 times here with 1000 time in any language supporting operators like "+=" or ".=".
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: How to convert a HEX value to a char?

Sun May 08, 2016 8:48 pm

In that case I will leave it as is, unless you have comments on the current script at https://gist.github.com/jpluimers/f667a ... f1eeda2c2f

Who is online

Users browsing this forum: No registered users and 13 guests