# Lua tip - concatenating sysex bytes

**URL:** https://forum.electra.one/t/lua-tip-concatenating-sysex-bytes/3459
**Category:** General discussion
**Created:** [December 22, 2024, 4:23pm UTC](https://forum.electra.one/t/lua-tip-concatenating-sysex-bytes/3459 "2024-12-22T16:23:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bramdvries](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/bramdvries/32/3540_2.png) [@bramdvries](https://forum.electra.one/u/bramdvries)
#### Post date: [December 22, 2024, 4:23pm UTC](https://forum.electra.one/t/lua-tip-concatenating-sysex-bytes/3459/1 "2024-12-22T16:23:02Z")

</div>

Maybe this is known to everyone, but then, maybe it isn’t.  
A control in my Kronos PolysixEx synth, Drum Track Shift (value ranges from -24 to +24), needs a very special value. The value consists of 3 bytes, and doesn’t follow a very logical pattern. Using Midi Monitor in the Preset Editor, the Kronos generated these values:  
Kronos value = SysEx bytes  
-24 = 7Fh 7Fh 68h  
.  
-1 = 7Fh 7Fh 7Fh  
0 = 00h 00h 00h  
+1 = 00h 00h 01h  
.  
+24 = 00h 00h 18h

So the first two SysEx value bytes are changed depending on postive or negative Kronos values. Furthermore, the third byte is not a very logical range:  
-24 = 104  
-1 = 127  
0=0  
1 = 1  
24 = 24

The solution was to create Lua function, leveraging midi.sysexsend, but then I had to concetanate different parts of the complete SysEx message. Online I found a solution called table\_fuse and it did the job perfectly:

– Korg Kronos  
SysExDeviceHeader = {0x42, 0x30, 0x68, 0x6E}  
local table\_fuse = function (…)  
– This function, created by zero1nx on [Stackoverflow.com](http://Stackoverflow.com), concatenates two tables  
– or better put in the context of SysEx, two arrays of Hex numbers  
local result = {}  
local i = 1

```
for _, obj in next, {...} do
    if type(obj) ~= "table" then
        result[i] = obj
        i = i + 1
    else
        for k, v in next, obj do
            if type(k) ~= "number" then
                result[k] = v
            else
                result[i] = v
                i = i + 1
            end
        end
    end
end

return result

```

end

function DrmPttrnShftValue(valueObject, value)  
– this function takes the input of the encoder and recalcules the correct value accepted by the Kronos DrumPattern Shift paramater  
– (-24 = 0x7F 0x7F 0x68, -1 = 0x7F 0x7F 0x7F, 0 = 0x00 0x00 0x00, +1 = 0x00 0x00 0x01, +24 = 0x00 0x00 0x18)  
– value range from the encoder is -24 to 24  
– The Sign Mode needs to be something else than “Unsigned”  
local SysExTargetID = {0x00, 0x00, 0x00, 0x09, 0x00}  
local NegativeNumberPrefix = {0x7F, 0x7F}  
local PositiveNumberPrefix = {0x00, 0x00}  
local ValuePrefix = {}  
local CalcedValue = {}

```
print("ReceivedValue: " .. string.format("%i", value))

if value >= 0 then
    ValuePrefix = PositiveNumberPrefix
    CalcedValue = value
else
    ValuePrefix = NegativeNumberPrefix
    CalcedValue = (value + 128)
end 
print("CalcedValue: " .. string.format("%i", CalcedValue) .. " = " .. string.format("%x", CalcedValue) .. "h")

midi.sendSysex (PORT_1 , table_fuse(SysExDeviceHeader,SysExTargetID, ValuePrefix,CalcedValue) )

```

end

---

<div class="post-metadata">

### Author: ![NewIgnis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/newignis/32/767_2.png) [@NewIgnis](https://forum.electra.one/u/NewIgnis)
#### Post date: [December 22, 2024, 4:52pm UTC](https://forum.electra.one/t/lua-tip-concatenating-sysex-bytes/3459/2 "2024-12-22T16:52:12Z")

</div>

It’s a logic Korg takes multiple times :

- the positive values start at 00h (the minimum) and go upwards.
- the negative numbers start of 7Fh 7Fh or 7Fh 7Fh 7Fh (whatever is the maximum) and go downwards

---

<div class="post-metadata">

### Author: ![bramdvries](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/bramdvries/32/3540_2.png) [@bramdvries](https://forum.electra.one/u/bramdvries)
#### Post date: [December 22, 2024, 7:58pm UTC](https://forum.electra.one/t/lua-tip-concatenating-sysex-bytes/3459/3 "2024-12-22T19:58:10Z")

</div>

Good to know, thanks for chiming in!
