View Single Post
Old 10-17-2015, 02:03 AM   #66
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,686
Default Storing Text in variables

encode_string() / decode_string() stores / retrieves four characters of text in a variable that can be assigned to a (invisible) slider and hence be managed by the DAW.

encode_5() and decode_5() handle 20 characters in a very trivial way.

Improvement appreciated.

-Michael


Code:
function encode_string(s) local(c_s, i) (
  i = 0;
  c_s = 0;
  while(
    c_s = c_s << 8;
    c_s = c_s + str_getchar(s, 4-i);  
    i = i+1;
    i < 5;
  );
  c_s;
);

function encode_5(r*, s) (
  r._1 = encode_string(strcpy_substr(#, s, 0, 4));
  r._2 = encode_string(strcpy_substr(#, s, 4, 4));
  r._3 = encode_string(strcpy_substr(#, s, 8, 4));
  r._4 = encode_string(strcpy_substr(#, s, 12, 4));
  r._5 = encode_string(strcpy_substr(#, s, 16, 4));
);

function decode_string(c_s) local (i, c_ss, _) (
  i = 0;
  strcpy (_, "");
  while(
    c_ss = c_s & 0xFF;
    c_s = c_s >> 8;
    c_ss >= 32 ? (
      str_setchar(_, -0.25, c_ss);
        i += 1;
    );  
    (i < 4) & (c_ss >= 32);
  );
  _;
);

function decode_5(r*) (  
  #__ =  decode_string(r._1);
  #__ += decode_string(r._2);
  #__ += decode_string(r._3);
  #__ += decode_string(r._4);
  #__ += decode_string(r._5);
);
Edit: Improved code now using parameters and return results instead of dedicated text variables and using the undocumented (but seemingly perfectly alowwable) "r*" notation for namespace parameters.

Still not happy with the non-local #__ intermediate String variable, though.

-Michael

Last edited by mschnell; 11-05-2015 at 02:37 PM.
mschnell is offline   Reply With Quote