ASCIIEncoding.GetString method

  • Thread starter Thread starter Paul K
  • Start date Start date
P

Paul K

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
SHBrowseForFolders 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.
 
Paul K said:
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
SHBrowseForFolders 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.

Why not just search for '\0' using IndexOf, and then call
myString.Substring (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.
My question is: Does anyone know what the Unicode
value '0' tranlates into in ASCII?
0.

When looking at the
returned string value from GetString, whatever value the
Unicode 0 is translated into is still there, but not
visible.

Instead of looking at ait in the debugger, dump the values out (as
integers) to the console.
 
Mattias:
Why don't you retrieve it directly into a string instead? How did you
declare the BROWSEINFO struct?

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 SHGetPathFromIDList 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
 
Back
Top