title: Delphi - Problem With Set String and PAnsiChar and Other Strings not Displaying
body: <p>I was getting <a href="https://stackoverflow.com/questions/628965/delphi-accessing-data-from-dynamic-array-that-is-populated-from-an-untyped-poin/629213#629213">advice from Rob Kennedy</a> and one of his suggestions that greatly increased the speed of an app I was working on was to use <code>SetString</code> and then load it into the VCL component that displayed it.</p>
  
  <p>I'm using Delphi 2009 so now that PChar is Unicode,</p>
  
  <pre><code>SetString(OutputString, PChar(Output), OutputLength.Value);
  edtString.Text := edtString.Text + OutputString;
  </code></pre>
  
  <p>Works and I changed it to PChar myself but since the data being moved isn't always Unicode in fact its usually ShortString Data.... so onto what he actually gave me to use:</p>
  
  <pre><code>SetString(OutputString, PAnsiChar(Output), OutputLength.Value);
  edtString.Text := edtString.Text + OutputString;
  </code></pre>
  
  <p>Nothing shows up but I check in the debugger and the text that normally appeared the way I did it building 1 char at a time in the past was in the variable.</p>
  
  <p>Oddly enough this is not the first time I ran into this tonight. Because I was trying to come up with another way, I took part of his advice and instead of building into a VCL's TCaption I built it into a string variable and then copied it, but when I send it over nothing's displayed. Once again in the debugger the variable that the data is built in... has the data.</p>
  
  <pre><code>for I := 0 to OutputLength.Value - 1 do
  begin
    OutputString := OutputString + Char(OutputData^[I]);
  end;
  edtString.Text := OutputString;
  </code></pre>
  
  <p>The above does not work but the old slow way of doing it worked just fine....</p>
  
  <pre><code>for I := 0 to OutputLength.Value - 1 do
  begin
    edtString.Text := edtString.Text + Char(OutputData^[I]);
  end;
  </code></pre>
  
  <p>I tried making the variable a ShortString, String and TCaption and nothing is displayed.  What I also find interesting is while I build my hex data from the same array into a richedit it's very fast while doing it inside an edit for the text data is very very slow.  Which is why I haven't bothered trying to change the code for the richedit as it works superfast as it is.</p>
  
  <p>Edit to add - I think I sorta found the problem but I have no solution.  If I edit the value in the debugger to remove anything that can't be displayed (which by the old method used to just not display... not fail) then what I have left is displayed.  So if it's just a matter of getting rid of bytes that were turned to characters that are garbage how can I fix that?</p>
  
  <p>I basically have incoming raw data from a SCSI device that's being displayed hex-editor style. My original slow style of adding one char at a time successfully displayed strings and Unicode strings that did not have Unicode-specific characters in them.  The faster methods even if working won't display ShortStrings one way and the other way wont display UnicodeStrings that aren't using non 0-255 characters.  I really like and could use the speed boost but if it means sacrificing the ability to read the string... then what's the point in the app?</p>
  
  <p>EDIT3 - Alright now that I've figured out that 0-31 is control char and 32 and up is valid I think I'm gonna make an attempt to filter char and replace those not valid with a . which is something I was planning on doing later to emulate hex editor style.</p>
  
  <p>If there are any other suggestions I'd be glad to hear about them but otherwise I think I can craft a solution that's faster than the original and does what I need it to at the same time.</p>