有几种不同的方法可以访问所需的精度级别。
具有 64 位的系统long
通常具有 128 位long long
的 s。请注意,您链接的文章说“至少64 位”。sizeof(long long)
如果没有什么可做的,值得检查一下。
假设这不是您正在使用的,您将不得不仔细查看 raw PyLongObject
,这实际上是typedef
私有_longobject
结构的 a 。
原始位可通过ob_digit
字段访问,长度由 给出ob_size
。数字的数据类型,以及它们持有的实际靴子数量由typedef
digit
和 宏给出PYLONG_BITS_IN_DIGIT
。后者必须小于8 * sizeof(digit)
、大于 8 和 5 的倍数(所以 30 或 15,取决于您的构建方式)。
幸运的是,C API 中有一个“未记录”的方法可以为你复制数字的字节:_PyLong_AsByteArray
. 中的评论longobject.h
写道:
/* _PyLong_AsByteArray: Convert the least-significant 8*n bits of long
v to a base-256 integer, stored in array bytes. Normally return 0,
return -1 on error.
If little_endian is 1/true, store the MSB at bytes[n-1] and the LSB at
bytes[0]; else (little_endian is 0/false) store the MSB at bytes[0] and
the LSB at bytes[n-1].
If is_signed is 0/false, it's an error if v < 0; else (v >= 0) n bytes
are filled and there's nothing special about bit 0x80 of the MSB.
If is_signed is 1/true, bytes is filled with the 2's-complement
representation of v's value. Bit 0x80 of the MSB is the sign bit.
Error returns (-1):
+ is_signed is 0 and v < 0. TypeError is set in this case, and bytes
isn't altered.
+ n isn't big enough to hold the full mathematical value of v. For
example, if is_signed is 0 and there are more digits in the v than
fit in n; or if is_signed is 1, v < 0, and n is just 1 bit shy of
being large enough to hold a sign bit. OverflowError is set in this
case, but bytes holds the least-significant n bytes of the true value.
*/
您应该能够获得类似的 UUID
PyLongObject *mylong;
unsigned char myuuid[16];
_PyLong_AsByteArray(mylong, myuuid, sizeof(myuuid), 1, 0);