有没有办法存储无符号长的核心数据? [英] Is there any way to store unsigned long in core data?
问题描述
CoreData提供了Integer 16,Integer 32和Integer 64存储,但不支持任何符号限定符。您可以将无符号整数(32位)存储为有符号长整型(64位),并确保该值保留全范围,但无符号长整形似乎需要一个128位有符号整数存储,当然,支持CoreData。有没有什么办法,然后存储unsigned long在coreData?
[上一个评论晋升为回答]
听起来像是对你很重要的位模式,而不是整数值本身。你可以把它存储为一个有符号的 - 只是把它转换为C有符号< - >无符号转换不强制数学正确性,只保留位。
是在(Obj-)C(++)中,您可以将无符号整数值存储到具有等效有符号整数类型的变量中,反之亦然。 C转换从signed - > unsigned by定义等同于使用2的补码整数时的位复制,两种类型是相同的大小。换句话说,无符号 - >有符号,是实现定义 - 在实践中,通常 意思是位拷贝。 ang GCC使用一个位复制为两个,但如果你想绝对肯定可以使用联合
: unsigned long r;
long l;
r =(unsigned long)l; //将永远工作(可选)
//下面是l =(long)r(可选的)没有implementation defined的风险
{union {long sValue; unsigned long uValue; } tmp; tmp.uValue = r; l = tmp.sValue;}
但是我真的怀疑任何人会! (注意:Clang至少会将其编译为直接赋值(位复制)。)
CoreData provides Integer 16, Integer 32 and Integer 64 storage, but doesn't support any sign qualifiers. You can store an unsigned int (32 bit) as a signed long (64 bit) and make sure the value is preserved for the full range, but an unsigned long seems to require a 128 bit signed integer to store, which of course isn't supported by CoreData. Is there any way then to store unsigned long in coreData?
[Previous comment promoted to answer]
Sounds like it is the bit pattern which is important to you and not the integer value per se. You can store it as a signed - just cast it as C signed<->unsigned casts don't enforce mathematical correctness and just preserve the bits. Cast it back to use it.
Follow up question:
In general yes in (Obj-)C(++) you can store an unsigned integer value into a variable with the equivalent signed integer type, and vice-versa. C casts from signed -> unsigned by definition equate to a bit-copy when using 2's complement integers and the two types are the same size. Going the other way, unsigned -> signed, is "implementation defined" - which in practice usually means a bit-copy. Clang & GCC use a bit-copy for both, but if you want to be absolutely certain you can use a union
:
unsigned long r;
long l;
r = (unsigned long)l; // will always work (cast optional)
// following is l = (long)r (cast optional) without "implementation defined" risk
{ union { long sValue; unsigned long uValue; } tmp; tmp.uValue = r; l = tmp.sValue;}
But seriously I doubt anybody would! (Note: Clang at least will compile it down to a straight assignment (bit-copy).)
这篇关于有没有办法存储无符号长的核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!