我们有一个Web服务,它检索查找信息,然后将数据表返回给用户。我们还想创建数据表的哈希值并将其与数据表一起返回。由于我一次只能从Web服务返回一个项目,这将是我可以返回两者的最简单方法。我不想创建列表或类,而是想要数据表。而且我不确定我是否想在数据表中包含哈希... 现在我有一个调用来检索数据表。然后我再次拨打另一个电话来获取哈希值,然后我会比较它是否正确。我这样做是为了验证数据完整性,只是想知道是否有更好的选择。
答案 0 :(得分:0)
您的方法必须返回具有这两个属性的类对象。该服务将创建一个新的double
对象并填充属性并返回此类实例。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
struct rational {
uint64_t n;
uint64_t d;
unsigned char sign : 1;
};
double uint64_as_double (uint64_t a)
{
double res;
#if defined (__cplusplus)
memcpy (&res, &a, sizeof (res));
#else /* __cplusplus */
volatile union {
double f;
uint64_t i;
} cvt;
cvt.i = a;
res = cvt.f;
#endif /* __cplusplus */
return res;
}
#define ADDcc(a,b,cy,t0,t1) (t0=(b), t1=(a), t0=t0+t1, cy=t0<t1, t0=t0)
#define ADDC(a,b,cy,t0,t1) (t0=(b)+cy, t1=(a), t0+t1)
#define SUBcc(a,b,cy,t0,t1) (t0=(b), t1=(a), cy=t1<t0, t1-t0)
double rational2double (struct rational a)
{
uint64_t dividend, divisor, quot, rem, t0, t1, cy, res, expo;
int sticky, round, odd, sign, i;
dividend = a.n;
divisor = a.d;
sign = a.sign;
/* handle special cases */
if ((dividend == 0) && (divisor == 0)) {
res = 0xFFF8000000000000ULL; /* NaN INDEFINITE */
} else if (dividend == 0) {
res = (uint64_t)sign << 63; /* zero */
} else if (divisor == 0) {
res = ((uint64_t)sign << 63) | 0x7ff0000000000000ULL; /* Inf */
}
/* handle normal cases */
else {
quot = dividend;
rem = 0;
expo = 0;
/* normalize operands using 128-bit shifts */
while (rem < divisor) {
quot = ADDcc (quot, quot, cy, t0, t1);
rem = ADDC (rem, rem, cy, t0, t1);
expo--;
}
/* integer bit of quotient is known to be 1 */
rem = rem - divisor;
quot = quot + 1;
/* generate 53 more quotient bits */
for (i = 0; i < 53; i++) {
quot = ADDcc (quot, quot, cy, t0, t1);
rem = ADDC (rem, rem, cy, t0, t1);
rem = SUBcc (rem, divisor, cy, t0, t1);
if (cy) {
rem = rem + divisor;
} else {
quot = quot + 1;
}
}
/* round to nearest or even */
sticky = rem != 0;
round = quot & 1;
quot = quot >> 1;
odd = quot & 1;
if (round && (sticky || odd)) {
quot++;
}
/* compose normalized IEEE-754 double-precision number */
res = ((uint64_t)sign << 63) + ((expo + 64 + 1023 - 1) << 52) + quot;
}
return uint64_as_double (res);
}