将byte 或object转换为GUID
我为对象数据类型分配了一些值,比如
object objData =dc.GetDirectoryEntry().Properties["objectGUID"].Value;
这个对象重新调整像{byte[16]} [0]: 145 [1]: 104 [2]: 117 [3]: 139 [4]: 124 [5]: 15 [6]: 255 [7]: 68 [8]: 142 [9]: 159 [10]: 208 [11]: 102 [12]: 148 [13]: 157 [14]: 179 [15]: 75
然后我将这个对象转换为byte [],就像
byte[] binaryData = objData as byte[];
它也将返回如{byte[16]} [0]: 145 [1]: 104 [2]: 117 [3]: 139 [4]: 124 [5]: 15 [6]: 255 [7]: 68 [8]: 142 [9]: 159 [10]: 208 [11]: 102 [12]: 148 [13]: 157 [14]: 179 [15]: 75
然后我从byte []转换hex值,如,
string strHex = BitConverter.ToString(binaryData);
它会像**91-68-75-8B-7C-0F-FF-44-8E-9F-D0-66-94-9D-B3-4B**
..但是我需要输出像GUID格式,我怎么能实现这个目标?
如何使用带有字节数组的Guid
构造函数 ?
Guid guid = new Guid(binaryData);
(如果需要,您可以使用Guid.ToString()
以文本forms获取它。)
长forms将是( 在此输入链接描述 ):
public static string ConvertGuidToOctectString(string objectGuid) { System.Guid guid = new Guid(objectGuid); byte[] byteGuid = guid.ToByteArray(); string queryGuid = ""; foreach (byte b in byteGuid) { queryGuid += @"\" + b.ToString("x2"); } return queryGuid; }
byte[] binaryData = objData as byte[]; string strHex = BitConverter.ToString(binaryData); Guid id = new Guid(strHex.Replace("-", ""))
System.DirectoryServices.DirectoryEntry
类具有用于此目的的属性Guid
– 无需通过Properties
访问objectGUID属性。