Getting string from [] byte in C #
I have a slightly weird problem. I have a form with a shortcut in it to output text at specific points in the program instead of console output. Given the following code:
result = SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref tBuff,
(uint)SPDRP.DEVICEDESC,
out RegType, ptrBuf,
buffersize, out RequiredSize);
if (!result)
{
errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
statusLabel.Text += "\nSetupDiGetDeviceRegistryProperty failed because "
+ errorMessage.ToString();
}
else
{
statusLabel.Text += "\nPtr buffer length is: " + ptrBuf.Length.ToString();
sw.WriteLine(tCode.GetString(ptrBuf) );
sw.WriteLine("\n");
// This is the only encoding that will give any legible output.
// Others only show the first character "U"
string tmp = tCode.GetString(ptrBuf) + "\n";
statusLabel.Text += "\nDevice is: " + tmp + ".\n";
}
I only get one hardware ID on the label. This piece of code is at the end of my loop. in the 1st it made me think my loop is some kind of hanging, but when I decided to pipe the output to a file, I get almost what I want and the output is outside the loop. Can anyone tell me what's going on here? All I want is to get a string representing the hardware ID from the [] byte ( ptrBuf ). Can someone explain what is going on here please? My working environment is MSVstudio 2008 express. In windows 7.
thanks
a source to share
You didn't show which one tCode
, unfortunately.
Looking at the docs for the API call it looks like it should be filled with REG_SZ. I suspect Unicode i.e.
string property = Encoding.Unicode.GetString(ptrBuf, 0, RequiredSize);
should convert it.
However, if you are expecting multiple values, I am wondering if this is a '\0'
parsed string: trying to output that in a Win32 control it will indeed stop at the first one '\0'
.
Try the following:
string property = Encoding.Unicode.GetString(ptrBuf, 0, RequiredSize);
.Replace('\0', ' ');
It should (if I'm guessing correctly) space - separate the values.
a source to share
Sorry I had to say. UnicodeEncoding tCode = new UnicodeEncoding();
and thanks to skeet, I was not aware of this little information about Win32 controls. I will do my best to fix this. I have not tried to implicitly convert bytes to characters (or strings). I will do my best to be more detailed in the future.
Thanks everyone for the answer.
a source to share
You cannot implicitly convert a byte to a string. You must choose an encoding method (possibly Unicode or ASCII) for conversion. A byte stores a numerical value that can represent a character (or some other data), but inherently means nothing. This is the philosophical equivalent of converting an integer to a string. You can do a direct conversion of the value or you can get some value from the value (i.e. Using ASCII table: 13 = TAB).
The return value of the function you specified will most likely return a byte array that represents some string value, however you need to find an appropriate encoding method to convert it to a usable string.
Hope it helped!
Eric
a source to share