Function StringToHexString(str : String) Returns String

It seems that other users are interested in a StringToHexString(str : String) function which helps in displaying messages on MIDI devices by sending the appropriate SysEx based on an Hex string.

So here is the solution I used in the topic Gig Performer controls the RME software mixer TotalMix FX from an Icon Platform M+ motorized control surface. I am not particularly proud of this solution which was inititially only a quick and dirty hack, but it has proven to work quite well until now :grimacing:

   ASCII_STRING     : String;
   ASCII_HEX_STRING : String array;

   ASCII_STRING = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[Y]^_`abcdefghijklmnopqrstuvwxyz{|}><";
   ASCII_HEX_STRING = ["20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F","60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F"];

Function StringToHexString(str : String) Returns String
  var i      : Integer
  result = "";

  For i=0; i<Length(str) ; i=i+1 Do
    result = result+ASCII_HEX_STRING[IndexOfSubstring(ASCII_STRING, CopySubstring(str,i,1), True)];

Why until now? Has it stopped working for some reason?

By the way, I’m not giving anything away by telling you that the next major version of GP will have a built-in function to do this conversion.

I said “until now” because it was not specifically and intensively tested… until now. Perhaps because I secretely hoped that something similar could appear in a future GP version :stuck_out_tongue_winking_eye:
But, no, it never stopped working. And it is quick enough to display varying values on the LCD of my control surface.

1 Like