Reply To: How to get label content and send it via SysEx?

Home Forums General Programming How to get label content and send it via SysEx? Reply To: How to get label content and send it via SysEx?

#83204
goodweather
Participant
  • Topics: 45
  • Replies: 550
  • Total: 595
  • ★★★

Instead to isolate each character and treating it with an if, you can treat the complete string at once with:

--
-- Returns HEX representation of a DECimal number
--
dec2hex = function(num)

    local hexstr = '0123456789ABCDEF'
    local s = ''

    while num > 0 do
        local mod = math.fmod(num, 16)
        s = string.sub(hexstr, mod+1, mod+1) .. s
        num = math.floor(num / 16)
    end
    if s == '' then s = '0' end
    return s

end

--
-- 	For the conversion of a DECimal number to anything else
--	it is just needed to use the built-in Lua function tonumber(string, base)
--
--	print(tonumber("0100",2))
--	4
-- 	print(tonumber("3F",16))
-- 	63

--
-- Returns HEX representation of a String
--
function str2hex(str)
    local hex = ''
    while #str > 0 do
        local hb = dec2hex(string.byte(str, 1, 1))
        if #hb < 2 then hb = '0' .. hb end
        hex = hex .. hb
        str = string.sub(str, 2)
    end
    return hex
end

Then to use it it is as simple as:
str2hex(txtLayerBName:getComponent():getProperty(“uiLabelText”))

Ctrlr