Home › Forums › General › Programming › transform ASCII text to hex?
Tagged: ascii
- This topic has 12 replies, 4 voices, and was last updated 11 years, 2 months ago by Stoner.
-
AuthorPosts
-
November 10, 2012 at 8:28 pm #773
Is there a built-in function that allows this?
I am trying to convert a string of text coming from a textinputwindow into hex so I can send this out for updating preset name characters.November 12, 2012 at 2:49 pm #4997So i cooked up a way to do this but when i try to send out the midi message i get an "unable to cast error"… here’s the code id have until now, all works except getting the midi message out, gives me a "unable to make cast" error.
From what i gathered here it has something to do with string types, i have no idea how to work around this one.[code:20eyzqs6]–
— Called when a modulator value changes
— @modulator http://ctrlr.org/api/class_ctrlr_modulator.html
— @newValue new numeric value of the modulator
—
setSampleName = function(modulator, newValue)–make a dialogwindow to enter a samplename //working kinda — button actions todo
c = panel:getComponent("setNamesLabel")
textwindow = utils.askForTextInputWindow("enter sample name", "Maximum 16 characters, surplus will be discarded – Do not forget to save", "", "", false, "ok", "cancel")
c:setPropertyString("uiLabelText", textwindow)
console(string.format("This was typed in the inputwindow"))
console(string.format(textwindow))–grab first 16 characters as decimal value ie ascii id //working
ch1 = string.byte(textwindow, 1)
ch2 = string.byte(textwindow, 2)
ch3 = string.byte(textwindow, 3)
ch4 = string.byte(textwindow, 4)
ch5 = string.byte(textwindow, 5)
ch6 = string.byte(textwindow, 6)
ch7 = string.byte(textwindow, 7)
ch8 = string.byte(textwindow,
ch9 = string.byte(textwindow, 9)
ch10 = string.byte(textwindow, 10)
ch11 = string.byte(textwindow, 11)
ch12 = string.byte(textwindow, 12)
ch13 = string.byte(textwindow, 13)
ch14 = string.byte(textwindow, 14)
ch15 = string.byte(textwindow, 15)
ch16 = string.byte(textwindow, 16)–fill empty values with space value to avoid nil value errors = ascii 32 //working
if ch1 == nil then ch1 = 32 end
if ch2 == nil then ch2 = 32 end
if ch3 == nil then ch3 = 32 end
if ch4 == nil then ch4 = 32 end
if ch5 == nil then ch5 = 32 end
if ch6 == nil then ch6 = 32 end
if ch7 == nil then ch7 = 32 end
if ch8 == nil then ch8 = 32 end
if ch9 == nil then ch9 = 32 end
if ch10 == nil then ch10 = 32 end
if ch11 == nil then ch11 = 32 end
if ch12 == nil then ch12 = 32 end
if ch13 == nil then ch13 = 32 end
if ch14 == nil then ch14 = 32 end
if ch15 == nil then ch15 = 32 end
if ch16 == nil then ch16 = 32 end–make memoryblock and dump character bytes inthere //working
namedata = CtrlrLuaMemoryBlock()
namedata:append(ch1)
namedata:append(ch2)
namedata:append(ch3)
namedata:append(ch4)
namedata:append(ch5)
namedata:append(ch6)
namedata:append(ch7)
namedata:append(ch8)
namedata:append(ch9)
namedata:append(ch10)
namedata:append(ch11)
namedata:append(ch12)
namedata:append(ch13)
namedata:append(ch14)
namedata:append(ch15)
namedata:append(ch16)–convert to hex and print to console //working
x = namedata:toHexString(1)
console(string.format("here comes hex"))
console(string.format(x))–create and send name command 09 – TODO tie lsb and msb to selectors // "unable to make cast"
–panel:sendMidiMessageNow(CtrlrMidiMessage({0xF0; 0x18, 0x21, 0x01, 0x55, 0x09, 0x00, 0x00, x, 0xF7}))end[/code:20eyzqs6]
Anyone have ideas?
November 14, 2012 at 8:22 pm #4998It’s a long shot but maybe try a different variable name besides "x"?
also you could try formatting your send name command line to be something like:
[code:1ry5qc7e]
blah = CtrlrMidiMessage({0xF0; 0x18, 0x21, 0x01, 0x55, 0x09, 0x00, 0x00, x, 0xF7}) –create the message
console(string.format(blah)) –just to verify the message being sent in the console
panel:sendMidiMessageNow(blah) –send the message
[/code:1ry5qc7e]That format seems to work for me when creating/sending midi messages.. It’s very similar to what you’re doing but it breaks it down into steps which might help when debugging this.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
November 15, 2012 at 7:31 pm #4999Thanks for thinking along, i have tried your ideas, but alas i keep bumping into the same error.
No matter how i break down the command(s), as soon as i want to insert the hexstring into the message construction it throws "unable to cast".The last resort i can think of is using an "invisible" lcd-strip and try to dump the memoryblock hex-ed output inthere & then fetch it from there to insert the values in the ougoing hexstring, but something tells me i will have the same error..
Or maybe some other way of coverting strings, but i am in the dark there not being a "real" programmer – my code propably already revealed this
It would be great if i got this to work, it would make my panel überfunctional
If/when i crack this i will keep this thread updated.
November 16, 2012 at 1:15 pm #5000I tried some more in various ways, still the same problem so imme let it go for now. Too bad because editing names from the ctrlr would be a great addition.
For what it’s worth, i ‘ve been digging around on and it’s propably due to the way luabind works,this thread gave me some clues[/url:9kprcm7c]
November 16, 2012 at 5:11 pm #5001This has to be simpler and it will be (it might already be) i need to have a look at it, you can do it using native LUA string class or JUCE added classes. I’ll come back with a solution asap.
November 17, 2012 at 10:00 pm #5002This will work in the next nightly with any string passed to the function as a second argument
[code:q5ks8lgv]
function stringToData(prefix, s, suffix)
data = prefix
block = MemoryBlock(0, true)for i=1, s:len() do
table.insert (data, string.byte(s:sub(i,i)))
endfor i=1, # suffix do
table.insert (data, suffix[i])
endblock:createFromTable(data)
return (CtrlrMidiMessage(block))
end
[/code:q5ks8lgv]example:
[code:q5ks8lgv]
m = stringToData({0xf0, 0x00, 0x10}, "fuck me", {0x23, 0xf7})
console (m:toString())LUA>> f0 00 10 66 75 63 6b 20 6d 65 23 f7
[/code:q5ks8lgv]November 18, 2012 at 3:40 pm #5003A version of the same function that should work with the current stable/nightly
[code:2sj95y7b]
function stringToData(prefix, s, suffix)
data = prefix
for i=1, s:len() do
table.insert (data, string.byte(s:sub(i,i)))
end
for i=1, # suffix do
table.insert (data, suffix[i])
end
return (CtrlrMidiMessage(data))
end
[/code:2sj95y7b]November 18, 2012 at 11:09 pm #5004I have just tested your function but alas, still the same error in v1180.
It does make it to the console correctly though, so the function you wrote here works great, and i will be able to use it elsewhere alot too so thanks for that ” title=”Very Happy” />
I will wait for the next build to try again and will update here on how it turns out.November 19, 2012 at 12:27 pm #5005panel:sendMidiMessageNow(CtrlrMidiMessage({0xF0; 0x18, 0x21, 0x01, 0x55, 0x09, 0x00, 0x00, x, 0xF7}))
this will never work
1) {0xF0; 0x18, – you can’t use ";" to seperate elements in a table you need to use ","
2) 0x00, 0x00, x, 0xF7} — "x" means nothing here, it’s not a valid HEX number, this isn’t the SysEx formula you are putting in, it’s raw data, you can’t place anything BUT numbers hereNovember 29, 2012 at 3:02 pm #5108I had spotted the “;” typo along the way while debugging, but thanks for pointing it out 😉
2- ok i see now, thanks for showing me a proper way to handle this !
December 20, 2012 at 12:04 pm #5208So i have had some time to work on my panel again recently, and i got this working 🙂
Seems I took a wrong approach with trying to use memoryblocks , as Atom suggested it is possible to do in native lua and this evades the “unable to make cast” problem.
This code does the trick:
setSampleName = function(modulator, newValue)
--make a dialogwindow to enter a samplename
c = panel:getComponent("setNamesLabel")
textwindow = utils.askForTextInputWindow("enter sample name", "do not use more than 16 characters", "please enter a name", "", false, "ok", "cancel")
c:setPropertyString("uiLabelText", textwindow)
console(string.format("This was typed in the set samplename inputwindow"))
console(string.format(textwindow))
--grab characters from texwindow's output (as dec)ch1 = string.byte(textwindow, 1)
ch2 = string.byte(textwindow, 2)
ch3 = string.byte(textwindow, 3)
ch4 = string.byte(textwindow, 4)
ch5 = string.byte(textwindow, 5)
ch6 = string.byte(textwindow, 6)
ch7 = string.byte(textwindow, 7)
ch8 = string.byte(textwindow, 8)
ch9 = string.byte(textwindow, 9)
ch10 = string.byte(textwindow, 10)
ch11 = string.byte(textwindow, 11)
ch12 = string.byte(textwindow, 12)
ch13 = string.byte(textwindow, 13)
ch14 = string.byte(textwindow, 14)
ch15 = string.byte(textwindow, 15)
ch16 = string.byte(textwindow, 16)--fill empty values with space value to avoid nil value errors = ascii 32 //working
if ch1 == nil then ch1 = 32 end
if ch2 == nil then ch2 = 32 end
if ch3 == nil then ch3 = 32 end
if ch4 == nil then ch4 = 32 end
if ch5 == nil then ch5 = 32 end
if ch6 == nil then ch6 = 32 end
if ch7 == nil then ch7 = 32 end
if ch8 == nil then ch8 = 32 end
if ch9 == nil then ch9 = 32 end
if ch10 == nil then ch10 = 32 end
if ch11 == nil then ch11 = 32 end
if ch12 == nil then ch12 = 32 end
if ch13 == nil then ch13 = 32 end
if ch14 == nil then ch14 = 32 end
if ch15 == nil then ch15 = 32 end
if ch16 == nil then ch16 = 32 end
--send name command bit = 09 // TODO tie two bits following 09 to sample number selection
namestr = string.format("f0 18 21 01 55 09 01 00 %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x F7", ch1,ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10, ch11, ch12, ch13, ch14, ch15, ch16)
console(string.format(namestr))panel:sendMidiMessageNow(CtrlrMidiMessage(namestr))
endFebruary 10, 2013 at 5:45 am #6294I realize this is a bit of an old post but I to needed to convert a sctring to MemoryBlock to use File:replaceWithData. I tried above and ended up writing my own, which does not use a table, but keeps it as string until conversion. Here it is and we start with some string s:
hex = string.format(“%02x”, string.byte(s))
for i = 2, #s do
hex = string.format( “%s %02x”, hex, string.byte(s,i) )
end
bytes = MemoryBlock()
bytes:loadFromHexString( hex )It is quite simple and short and most of it can be displayed using console(). It also could be converted in to a function:
convertStringToData = function( s )
hex = string.format(“%02x”, string.byte(s))
for i = 2, #s do
hex = string.format( “%s %02x”, hex, string.byte(s,i) )
end
data = MemoryBlock()
data:loadFromHexString( hex )
return( data )
endThis is just an FYI if anyone else is interested.
Stoner
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.