ASCIIEncoding.GetString method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul K

    #1

    ASCIIEncoding.GetString method

    I've run into a problem using this method that I have
    been able to work around, but I think that there is a
    more elegant way.

    I create an array of 256 bytes and use the
    SHBrowseForFold ers API to retrieve a folder path into the
    array. When I use the GetString method, the length of
    the returned string is always 256, even if the actual
    path length is only 10.

    Right now, I step through the byte array and count the
    number of 0's that occur, and then use the string's
    Remove method to remove that equal number of characters
    from the end of the string.

    My question is: Does anyone know what the Unicode
    value '0' tranlates into in ASCII? When looking at the
    returned string value from GetString, whatever value the
    Unicode 0 is translated into is still there, but not
    visible.
  • Jon Skeet

    #2
    Re: ASCIIEncoding.G etString method

    Paul K <shaul_ahuva@co mcast.net> wrote:[color=blue]
    > I've run into a problem using this method that I have
    > been able to work around, but I think that there is a
    > more elegant way.
    >
    > I create an array of 256 bytes and use the
    > SHBrowseForFold ers API to retrieve a folder path into the
    > array. When I use the GetString method, the length of
    > the returned string is always 256, even if the actual
    > path length is only 10.
    >
    > Right now, I step through the byte array and count the
    > number of 0's that occur, and then use the string's
    > Remove method to remove that equal number of characters
    > from the end of the string.[/color]

    Why not just search for '\0' using IndexOf, and then call
    myString.Substr ing (0, resultOfIndexOf ) to get the result? Or search
    for byte 0 before calling GetString, and call GetString with the form
    which takes parameters to specify the first byte to decode and the
    number of bytes to decode.
    [color=blue]
    > My question is: Does anyone know what the Unicode
    > value '0' tranlates into in ASCII?[/color]

    0.
    [color=blue]
    > When looking at the
    > returned string value from GetString, whatever value the
    > Unicode 0 is translated into is still there, but not
    > visible.[/color]

    Instead of looking at ait in the debugger, dump the values out (as
    integers) to the console.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Paul K

      #3
      Re: ASCIIEncoding.G etString method

      Mattias:
      [color=blue]
      >Why don't you retrieve it directly into a string[/color]
      instead? How did you[color=blue]
      >declare the BROWSEINFO struct?[/color]

      At first, I chose to retrieve it into an array of bytes
      because I know a little about C++ and the Win32 API, but
      not a whole lot, and I was following some advice I was
      given. After a little bit of checking around, I see that
      the SHGetPathFromID List needs a pointer to a buffer for
      the path, but I am not able to create a pointer to a
      string (according to the compiler, unsafe pointers or
      references to managed types are invalid). I have my
      BROWSEINFO struct declared as follows:

      private struct BROWSEINFO
      {
      public int hwndOwner;
      public int pidlRoot;
      public byte* pszDisplayName;
      public byte* lpszTitle;
      public uint ulFlags;
      public int lpfn;
      public int lparam;
      public int imImage;
      }

      Is there something that I'm missing? If so, I would
      greatly appreciate any suggestions. Thanks!

      Paul

      Comment

      Working...