Author Topic: ResultBufferToString function from Daniel - \0 padding  (Read 1823 times)

0 Members and 1 Guest are viewing this topic.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
ResultBufferToString function from Daniel - \0 padding
« on: October 14, 2009, 05:25:17 PM »
I found his "AcIo" project yesterday as I too neeed to tag entities with long strings of data.
I noticed in the ResultBufferToString function that it pads the string with \0's until the string is 256 chars long.
simply changing:
str = coder.GetString(fs.GetBuffer));
to
str = coder.GetString(fs.GetBuffer()).Trim('\0');
did the trick.
I don't deal with byte arrays much, so it took me a while to figure out.
Any more elegent solutions welcome, thx
James Maeding

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8735
  • AKA Daniel
Re: ResultBufferToString function from Daniel - \0 padding
« Reply #1 on: October 14, 2009, 09:21:18 PM »
thats fine or like this

Code: [Select]
   internal static string ResultBufferToString(ResultBuffer buf, bool decompress)
    {
      if (buf == null)
      {
        throw new ArgumentNullException("ResultBuffer buffer");
      }

      System.Text.ASCIIEncoding coder = new System.Text.ASCIIEncoding();
      string str = string.Empty;
      using (MemoryStream fs = new MemoryStream())
      {
        using (MemoryStream ms = new MemoryStream())
        {
          if (decompress)
          {
            ResultBufferToStream(buf, ms);
            Decompress(ms, fs);
          }
          else
          {
            ResultBufferToStream(buf, fs);
          }
        }
        str = coder.GetString(fs.GetBuffer());
      }
      return str.Trim('\0');
    }


BTY, I'm not sure what's adding the padding, I would bet it's the resbuf BinaryChunk, if it is, trimming the string is about the only option. goofing with the byte stream might produce unwanted results
« Last Edit: October 14, 2009, 09:39:56 PM by Daniel »