再present一个GUID作为一个整数集 [英] Represent a Guid as a set of integers

查看:109
本文介绍了再present一个GUID作为一个整数集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要重新present一个GUID作为一个整数集我将如何处理这种转换?我相处的字节数组重新GUID的presentation,并分解成可以转换回原始的GUID尽可能少的32位整数的思路思考。 code例子preferred ...

If I want to represent a guid as a set of integers how would I handle the conversion? I'm thinking along the lines of getting the byte array representation of the guid and breaking it up into the fewest possible 32 bit integers that can be converted back into the original guid. Code examples preferred...

此外,什么将结果整数数组的长度呢?

Also, what will the length of the resulting integer array be?

推荐答案

不知怎的,我有更多的乐趣做这种方式:

Somehow I had much more fun doing it this way:

byte[] bytes = guid.ToByteArray();
int[] ints = new int[bytes.Length / sizeof(int)];
for (int i = 0; i < bytes.Length; i++) {
    ints[i / sizeof(int)] = ints[i / sizeof(int)] | (bytes[i] << 8 * ((sizeof(int) - 1) - (i % sizeof(int))));
}

和转换回:

byte[] bytesAgain = new byte[ints.Length * sizeof(int)];
for (int i = 0; i < bytes.Length; i++) {
    bytesAgain[i] = (byte)((ints[i / sizeof(int)] & (byte.MaxValue << 8 * ((sizeof(int) - 1) - (i % sizeof(int))))) >> 8 * ((sizeof(int) - 1) - (i % sizeof(int))));
}
Guid guid2 = new Guid(bytesAgain);

这篇关于再present一个GUID作为一个整数集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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