Home Forums General Programming Removing spaces from ASCII text

Tagged: ,

This topic contains 4 replies, has 3 voices, and was last updated by lfo2vco lfo2vco 4 years, 2 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #13613
    lfo2vco
    lfo2vco
    Participant

    OK, can’t figure a way of doing this…

    I’m getting text from a uiLabel, this works fine. However the text come from the patch name of a Sysex Dump and there are 15 bytes allocated to the for this task. So if the name is twelve characters long the remaining three characters get filled with spaces thus:

    ASCII = "High Strings   "
    HEX   = "48 69 67 68 20 53 74 72 69 6e 67 73 20 20 20"

    Not a problem till I place this in the name field of a Save File Window then it becomes "High Strings .syx", which looks rubbish.

    So is there a crafty method I can use to remove more that one space in a row?

    #13620
    msepsis
    msepsis
    Participant

    I might not be explaining this clearly but here’s vaguely how I would approach it:
    Before placing HEX in the name field of a Save File Window, assign the value of each byte of HEX each to a unique variable. Check each variable starting from the last to see if it’s value is 20. If the last value in the sequence == 20, truncate it, then check the next and continue to check the next last variable’s value. By truncate I mean reconstruct HEX with your variables, but omit the last variable if it’s value equals 20.

    Might be a faster way though but this would get it done.

    • This reply was modified 4 years, 2 months ago by msepsis msepsis.

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #13625
    atom
    atom
    Moderator

    You have the String class at your disposal in Lua, so http://www.juce.com/juce/api/classString.html#ad3853992f01ae00f2e6ed2d4eda85fb6

    #13626
    lfo2vco
    lfo2vco
    Participant

    That’s sweet and should make the process much easier.

    Cheers guys.

    #13710
    lfo2vco
    lfo2vco
    Participant

    OK, I have been trying to make this work and I found this:

    -- trim whitespace from right end of string
    function trimr(s)
    return s:find'^%s*$' and '' or s:match'^(.*%S)'
    end

    I’m guessing this is related to the String Class Atom made reference to. This is how I got it to work in my method:

    nameData = L(PatchLabel:getProperty("uiLabelText"))
    
    function trim (s)
    return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
    end
    
    nameTrim = (trim(nameData))
    fileToWrite = utils.saveFileWindow("Save patch to disk", File(nameTrim), "*.syx", true)
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Comments are closed.