title: How to convert strings to array of byte and back
body: <p>4I must write strings to a binary MIDI file. The standard requires one to know the length of the string in bytes. As I want to write for mobile as well I cannot use AnsiString, which was a good way to ensure that the string was a one-byte string. That simplified things. I tested the following code:</p>
  
  <pre><code>TByte = array of Byte;
  
  function TForm3.convertSB (arg: string): TByte;
  var
     i: Int32;
  begin
     Label1.Text := (SizeOf (Char));
     for i := Low (arg) to High (arg) do
     begin
        label1.Text := label1.Text + ' ' + IntToStr (Ord (arg [i]));
     end;
  end; // convert SB //
  
  convertSB ('MThd');
  </code></pre>
  
  <p>It returns 2 77 84 104 100 (as label text) in Windows as well as Android. Does this mean that Delphi treats strings by default as UTF-8? This would greatly simplify things but I couldn't find it in the help. And what is the best way to convert this to an array of bytes? Read each character and test whether it is 1, 2 or 4 bytes and allocate this space in the array? For converting back to a character: just read the array of bytes until a byte is encountered &lt; 128?</p>