简体   繁体   中英

getting a string from a []byte in C#

I have a bit of a weird problem. I have a form with a Label in it for outputting text at certain 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 get just the one hardware ID output on the label. This piece of code is at the end of my loop. at 1st this made me think that my loop was some how hanging, but when I decided to direct output to a file I get almost what I want and the output outside the loop. Can anyone tell me what's going on here? All I want is to get the string representing the hardware ID from the []byte ( ptrBuf ). Can some explain what's going on here, please? My working environment is MSVstudio 2008 express. In windows 7.

Thanks

You haven't shown what tCode is, unfortunately.

Looking at the docs for the API call it looks like it should be populated with a REG_SZ. I suspect that's Unicode, ie

string property = Encoding.Unicode.GetString(ptrBuf, 0, RequiredSize);

should convert it.

However, if you're expecting multiple values, I wonder if it's a '\\0' -separated string: trying to output that in a Win32 control will indeed stop at the first '\\0' .

Try this:

string property = Encoding.Unicode.GetString(ptrBuf, 0, RequiredSize);
                                  .Replace('\0', ' ');

That should (if I'm guessing correctly) space-separate the values.

You need to specify an encoding:

// C# to convert a byte array to a string.
byte [] dBytes = ...
string str;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);

sorry I should've said. UnicodeEncoding tCode = new UnicodeEncoding(); and thanks skeet, I didn't know about that little info on Win32 controls. I'll make an effort to correct for that. I wasn't implicitly trying to convert the bytes into characters (or strings). I'll make an effort to be more detailed in the future.

Thanks all for replying.

You cannot implicitly convert a byte to a string. You must choose an encoding method (perhaps Unicode or ASCII) for the conversion. A byte stores a numerical value which can represent a character (or some other data), but inherently does not mean anything. It is the philosophical equivalent to converting an integer to a string. You can decide to do a direct conversion of the value or you can derive some meaning from the value (ie using an ASCII table: 13 = TAB).

The value being returned by the function you listed most likely returned a byte array that represents some string value, however it is up to you to find the relevant encoding method to convert it to a usable string.

Hope that helped!

Eric

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM