title: How to remove garbage characters when converting from Bytes Stream/buffer to String?
body: <p>Getting string of exact length from non-fixed length buffer of bytes from process memory</p>
  
  <p>I want to read a process(game) memory to get <strong>File Names(string)</strong> of maps the game has currently loaded in the process memory.</p>
  
  <p>The expected string is of no fixed length , the maximum possible value is 37 so i declared the buffer of length 37.</p>
  
  <p>The read part was easy by using Win32 API.</p>
  
  <pre><code>byte[] pBuffer = new byte[37];
  ReadProcessMemory((int)processHandle, (int)address, pBuffer, 37, ref read);
  </code></pre>
  
  <p>Here is the code to convert the bytes to string.</p>
  
  <pre><code>string map = Encoding.ASCII.GetString(pBuffer);
  </code></pre>
  
  <p>The output is fine but the string contains garbage characters hidden from display.</p>
  
  <p>When i tried to open the  file, <strong>FileExist(map)</strong> function return false. The files are there in the folder but i found out that the String.Length is 37 for every string i get from memory.</p>
  
  <p>E.g The file name is <strong>"abc.bms"</strong> and its lenght(String.Lenght) is <strong>37</strong>.</p>
  
  <p>Solution i tried so far :- </p>
  
  <pre><code>  // string = string.Substring(0, string.IndexOf('\0'));   
    // string = string.Replace('\0');
    // string = string.Trim();
  </code></pre>
  
  <p>The string length is not correct , i want to remove all those invisible characters from string ?</p>