views:

70

answers:

2

Hi I am trying to get device id of windows 7 phone using following code

 byte[] result = null;
 String id = null;
 object uniqueId;
 if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
     result = (byte[])uniqueId;

Problem is that I need this result in String. Can anyone tell me how to do this? Best Regards

+1  A: 
System.Text.Encoding.UTF8.GetString(myBytes, 0, myBytes.Length);

I haven't checked but I suppose this method is available on Windows Phone.

herzmeister der welten
When i do this, the result in debugger is shown in form of special characters.
Aqueel
This is the result i get after using the code you posted. Nʝ���Z�����ʻj�l"�
Aqueel
Is it specified somewhere that a "DeviceUniqueId" must be human-readable?
herzmeister der welten
Also you might try another encoding format than UTF8.
herzmeister der welten
+4  A: 
string myString = Convert.ToBase64String(result);

This function is available on the windows phone 7 platform

http://msdn.microsoft.com/en-us/library/dhx0d524(VS.95).aspx

And if you need the byte array again, just ask for it like this.

byte[] byteArray = Convert.FromBase64String(myString);

edit: Curt provided the correct way to convert back to a byte array

castis
Great!! That worked. thanks a lot castis.
Aqueel
FYI, in order to get the original bytes back from the Base 64 string you'll need to use Convert.FromBase64String(), not GetBytes() as shown above.
Curt Nichols