转换的GUID数字等效 [英] Convert GUID to Numeric Equivalent

查看:264
本文介绍了转换的GUID数字等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在问出口的用户列表从ActiveDirectory中,并将其转换的GUID为数字相当于显示器(所以我之后的ToString)。我做了一些搜索,但大部分的问题都在转换的东西为GUID,或将一个GUID来诠释什么愚蠢的,(我的理解整型太小等),这arn't真是再合适不过了。

我有AD的东西排序,导出的数据,但我不知道什么样的数据类型/格式使用到的GUID转换为,如果它甚至有可能。我没有真正困扰是什么精确的数字类型时,只要数量仍是独一无二(我的理解是GUID的arn't真正独特等)。

是基于十六进制格式的GUID,并没有任何合适的数值类型我可以将它们转换为显示器?

我使用VB.Net和.NET Framework 4,我用的BigInteger尝试过,但似乎进入负数也一样,我将使用如果我有,但宁可不要,如果有什么东西其他更合适的。

修改 下面是我的尝试:

使用GUID的:f02c7caf-7e5a-491B-9f23-9476174bdda1

这code:

 昏暗美孚的String =(新System.Numerics.BigInteger(adUserDirectoryRecord.Guid.ToByteArray()))。的ToString
 

据来如: -125127638954164478915246035839554388817

注意:我会以.csv格式输出这一次,我做了,另一支球队拿起并导入到另一个系统,这可能是为什么他们想在数字相当比字母数字格式。

解决方法:

正如会有不需要以往转换数值的GUID背面,BigInteger的是使用这种正确的方法。这种变化是要调整的ByteArray用它强制正值只:

 '获取从Active Directory记录用户的GUID,将其转换为字节数组,并调整其大小,以唯一力量正值:
昏暗bytGUIDBytes为字节()= adUserDirectoryRecord.Guid.ToByteArray()
Array.Resize(bytGUIDBytes,17)

转换用户的GUID字节成数字等效,并返回该数值的字符串版本。
返回新System.Numerics.BigInteger(bytGUIDBytes)的ToString
 

解决方案

数字就没有意义了在VB WRT 的Guid 。没有128位的数值类型的.NET。只有128位的类型是小数;但似乎没有任何意义转换的Guid 十进制。我会简单地使用的toByteArray 并显示该字节值(十六进制)。

替代地,确保给予的BigInteger阵列中的最后一个字节是零以获得阳性值。例如::

  VAR字节= Guid.NewGuid()的toByteArray()。
Array.Resize(REF字节,17);
VAR BIGINT =新的BigInteger(字节);
VAR值= bigInt.ToString();
 

I've been asked to export a list of users from ActiveDirectory, and Convert the GUIDs to a numeric equivalent for display (so I'll .ToString that after). I've done some searching but most of the questions are on converting something TO a GUID, or converting a GUID to Int or something silly, (I understand Ints are too small etc..), which arn't really appropriate.

I have the AD stuff sorted and the data exported, however I'm wondering what data type / format to use to convert the GUIDs to, and if it is even possible. I'm not really bothered what exact numeric type is used, as long as the number is still "unique" (I understand that GUIDs arn't really unique etc..).

Are GUIDs in hex based format, and is there any suitable numeric type I can convert them to for display?

I'm using VB.Net and .Net Framework 4. I've tried using BigInteger, but that seems to go into negative numbers too, which I'll use if I have to, but would rather not if there's something else more suitable.

Edit Here's what I tried:

Using a GUID of: f02c7caf-7e5a-491b-9f23-9476174bdda1

And this code:

Dim Foo As String = (New System.Numerics.BigInteger(adUserDirectoryRecord.Guid.ToByteArray())).ToString

It came out as: -125127638954164478915246035839554388817

Note: I'll be outputting this in .csv format once I'm done, for another team to pick up and import into another system, this is likely why they want it in numeric rather than alphanumeric format.

SOLUTION:

As there will be no need to ever convert the Numeric GUID back, BigInteger was the correct method to use for this. The change was to Resize the ByteArray to force it to Positive values only:

'Gets the User's GUID from the Active Directory record, converts it to a Byte Array and Resizes it to Force Positive Values Only:
Dim bytGUIDBytes As Byte() = adUserDirectoryRecord.Guid.ToByteArray()
Array.Resize(bytGUIDBytes, 17)

'Converts the User's GUID Bytes into a Numeric Equivalent, and Returns the String version of the Numerical value.
Return New System.Numerics.BigInteger(bytGUIDBytes).ToString

解决方案

"Numeric" doesn't make sense in VB w.r.t Guid. There is no 128-bit numeric types in .net. The only 128-bit type is Decimal; but that doesn't seem to make sense to convert a Guid to a Decimal. I would simply use ToByteArray and display the byte values as hex.

Alternatively, make sure the last byte in the array given to BigInteger is zero to get a postive value. e.g.::

var bytes = Guid.NewGuid().ToByteArray(); 
Array.Resize(ref bytes, 17); 
var bigInt = new BigInteger(bytes);
var value = bigInt.ToString();

这篇关于转换的GUID数字等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆