C# 将字节 [] 或对象转换为 GUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10862171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:33:48  来源:igfitidea点击:

Convert byte[] or object to GUID

c#active-directoryguid

提问by Manikandan Sethuraju

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

此对象返回值,如 {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

它也会像这样返回, {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,

然后我从 byte[] 转换十六进制值,例如,

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?

它会像**91-68-75-8B-7C-0F-FF-44-8E-9F-D0-66-94-9D-B3-4B**..一样返回,但我需要像 GUID 格式这样的输出,我怎样才能做到这一点?

采纳答案by Jon Skeet

How about using the Guidconstructor which takes a byte array?

如何使用Guid带有字节数组构造函数

Guid guid = new Guid(binaryData);

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

Guid.ToString()如果需要,您可以使用它以文本形式获取。)

回答by dahvyd

The System.DirectoryServices.DirectoryEntryclass has the property Guidfor this purpose - no need to access the objectGUID attribute through Properties.

System.DirectoryServices.DirectoryEntry类具有以下属性:Guid为了这个目的-不需要通过访问objectGUID属性Properties

回答by Michael Flyger

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

回答by André Voltolini

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

回答by Phillippe Santana

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 SQLiteConnectionStringBuilderto configure a Connection that understands your Guids as Guids:
  1. 您可以使用官方的 SQLite 提供程序而不是 Microsoft 的提供程序。使用它SQLiteConnectionStringBuilder来配置一个将你的 Guids 理解为 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/

这是官方的 SQLite 提供程序: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:
  1. 如果使用 Dapper,您可以使用此类自动将字节数组转换为 Guid,该类应在您的应用程序启动后立即注册:
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

来源:Dapper 问题 #718 - GitHub