SchemaFactor.Vst.MidiMapperX.MapNoteItem.StringToBytes C# (CSharp) Méthode

StringToBytes() public static méthode

Static member used to convert the hex strings into bytes. Used by the mapping code, as well as the entry dialog box for validation.
public static StringToBytes ( string OutputBytesString, byte channel, byte velocity ) : byte[]
OutputBytesString string
channel byte
velocity byte
Résultat byte[]
        public static byte[] StringToBytes(string OutputBytesString, byte channel, byte velocity)
        {
            lastOutputString = "";

            // Null string case - Return null
            if (OutputBytesString == null) return null;

            // Empty string case - Return null
            if (OutputBytesString.Equals("")) return null;

            // Limit channel
            channel = (byte)(channel & 0x0F);

            // Temporary array since BitConverter works on arrays.  
            byte[] temp = new byte[2];
            temp[0] = channel;
            temp[1] = velocity;

            // Replace N with channel and VV with velocity (uppercase is forced by dialog box)
            OutputBytesString = OutputBytesString.Replace(  "N", System.BitConverter.ToString(temp, 0, 1).Substring(1,1) );
            OutputBytesString = OutputBytesString.Replace( "VV", System.BitConverter.ToString(temp, 1, 1) );

            // Store result for debugging
            lastOutputString = OutputBytesString;            

            // Update byte representation
            string[] array_hex = OutputBytesString.Split(' ');
            byte[] array_bytes = new byte[array_hex.Length];

            for (int i = 0; i < array_hex.Length; i++)
            {
                // This could potentially throw a parsing exception.  Ignore here so it bubbles up to caller.
                array_bytes[i] = System.Convert.ToByte(array_hex[i], 16);
            }
            return array_bytes;               
        }      
    }

Usage Example

        private void MapNoteDetailsUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.OK)
            {
                FormToEntity();

                // Parse the strings with dummy substitution values to verify they're OK.  Don't use 0 as it may mask weird input ('NN33', for example)
                try
                {
                    MapNoteItem.StringToBytes(MIDIBytesOnTxt.Text, 15, 255);
                }
                catch // All, assume parsing errors
                {
                    this.MIDIBytesOnTxt.BackColor = System.Drawing.Color.Yellow;
                    e.Cancel = true;
                }

                try
                {
                    MapNoteItem.StringToBytes(MIDIBytesOffTxt.Text, 15, 255);
                }
                catch // All, assume parsing errors
                {
                    this.MIDIBytesOffTxt.BackColor = System.Drawing.Color.Yellow;
                    e.Cancel = true;
                }
            }
        }
All Usage Examples Of SchemaFactor.Vst.MidiMapperX.MapNoteItem::StringToBytes