Convert byte[] or object to GUID

浪子不回头ぞ 提交于 2019-12-04 16:53:40

问题


I assigned some value to object data type like,

object objData =dc.GetDirectoryEntry().Properties["objectGUID"].Value;

this object retun the value like {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

Then i casting this object to byte[], like

byte[] binaryData = objData as byte[];

It will also return like, {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

Then i convert the hex values from byte[] like,

string strHex = BitConverter.ToString(binaryData);

It will be return like **91-68-75-8B-7C-0F-FF-44-8E-9F-D0-66-94-9D-B3-4B**.. But i need the output like GUID format, How can i achieve this?


回答1:


How about using the Guid constructor which takes a byte array?

Guid guid = new Guid(binaryData);

(You can then use Guid.ToString() to get it in text form if you need to.)




回答2:


The long form would be (enter link description here):

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;
}



回答3:


byte[] binaryData = objData as byte[];
string strHex = BitConverter.ToString(binaryData);
Guid id = new Guid(strHex.Replace("-", ""))



回答4:


The System.DirectoryServices.DirectoryEntry class has the property Guid for this purpose - no need to access the objectGUID attribute through Properties.




回答5:


Though the manual casting suggested above works, there's a way to do this automatically.

  1. You can use the official SQLite provider instead of the one from Microsoft. Use its SQLiteConnectionStringBuilder to configure a Connection that understands your Guids as Guids:
var builder = new SQLiteConnectionStringBuilder("Data Source=./mydatabase.db") { BinaryGUID = true };
var connStr = builder.ToString();
return new SQLiteConnection(connStr);

Here's the official SQLite provider: https://www.nuget.org/packages/System.Data.SQLite.Core/

  1. If using Dapper, you can have it automatically convert byte arrays to Guid using this class, which should be registered once as soon as your app starts:
public class GuidTypeHandler : SqlMapper.TypeHandler<Guid>
{
    public override Guid Parse(object value)
    {
        var valueAsBytes = (byte[])value;
        return new Guid(valueAsBytes);
    }

    public override void SetValue(System.Data.IDbDataParameter parameter, Guid value)
    {
        var guidAsBytes = value.ToByteArray();
        parameter.Value = guidAsBytes;
    }
}

// And the registration in Startup.cs or equivalent:
SqlMapper.AddTypeHandler<Guid>(new GuidTypeHandler());

Source: Dapper Issue #718 - GitHub



来源:https://stackoverflow.com/questions/10862171/convert-byte-or-object-to-guid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!