Hex to Dec. calculation from a SysEx string

I read a a lot about calculation but wasn't able to find a solution.

Would it be possible to convert an entire SysEx string like the following example…

43 10 7F 1C 0C 30 5A 0F 01

… into decimal format? Ideally also comma-separated so that the result looks like…

67, 16, 127, 28, 12, 48, 90, 15, 01

Thanks in advance!

I hope these links will suggest a suitable approach.

To get each item in a collection:

For Each Action
How to parse User Input space-separated string to a For Each action?

Hex to decimal:

HEX Function

To add the commas:

Search_and_Replace

One approach:

Decimal versions of a line of hex numbers.kmmacros (3.4 KB)

Thanks @kevinb, that's what I tried. But except the "Search_and_Replace" I'm usually too stupid to just understand the examples. :roll_eyes:

1 Like

Thanks so much @ComplexPoint. I'm not able to understand the magic behind it but it works great! :grinning:

2 Likes
  1. Splitting on one or more spaces (\s+) gives us a JavaScript Array of sub-strings.
  2. We can always get a consistently modified copy (map) of a JS Array by using its .map method, and specifying the modification in an item => modifiedItem pattern.
  3. The modification here is that we take an existing hex string (let's call it 's'), obtain a copy prefixed with '0x' (signifiying Hex, in JS number strings), and apply the JS Number constructor to that prefixed string, to get a base-independent numeric value.

We now have a JS Array of base-independent Number values, which JS displays (or 'stringifies') by default in base 10, and when these whole Arrays get coerced to strings (the string passed back as a Keyboard Maestro variable), they become comma-delimited by default.

2 Likes

Thanks again for the explanation. Unfortunately, as already mentioned, this is far too high for my abilities. But of course there's always a lot to learn here. :wink:

2 Likes