使用 hessian 从 android 发送 double 到 php 时出现解析错误
我想使用 hessian 将 double 值从 Android 客户端发送到 PHP 服务器,但客户端上的 writeDouble 函数或服务器上的 parseDouble 函数有错误。 (我们正确地传输许多不同的数据类型,只有 double 给我们带来麻烦:))
double 值是经度和纬度,例如:
从 android 客户端发送:14,30485725402832
在服务器接收:1.0474191691834E-321
android 编码:
public void writeDouble(double value) throws IOException
{
long bits = Double.doubleToLongBits(value);
os.write('D');
os.write((byte) (bits >> 56));
os.write((byte) (bits >> 48));
os.write((byte) (bits >> 40));
os.write((byte) (bits >> 32));
os.write((byte) (bits >> 24));
os.write((byte) (bits >> 16));
os.write((byte) (bits >> 8));
os.write((byte) (bits));
}
php 解码:
function parseDouble($code, $num){
$bytes = $this->read(8);
if(HessianUtils::$littleEndian)
$bytes = strrev($bytes);
$double = unpack("dflt", $bytes);
return $double['flt'];
}
顺便说一句:我们还有一个 iPhone 客户端发送双 - 工作正常...
iphone 编码:
(void)encodeDouble:(double)realv forKey:(NSString*)key;
{
if (key) [self writeTypedObject:key];
[self writeChar:'D'];
[self writeInt64:(int64_t)(*((double*)(&realv)))];
}
I want to send a double value from my android client to the PHP server using hessian but either the writeDouble function on the client or the parseDouble function on the server has an error. (We transmit many different data types correctly, only the double give us trouble :))
The double values are longitude and latitude for example:
sent from android client: 14,30485725402832
received at server: 1.0474191691834E-321
android encoding:
public void writeDouble(double value) throws IOException
{
long bits = Double.doubleToLongBits(value);
os.write('D');
os.write((byte) (bits >> 56));
os.write((byte) (bits >> 48));
os.write((byte) (bits >> 40));
os.write((byte) (bits >> 32));
os.write((byte) (bits >> 24));
os.write((byte) (bits >> 16));
os.write((byte) (bits >> 8));
os.write((byte) (bits));
}
php decoding:
function parseDouble($code, $num){
$bytes = $this->read(8);
if(HessianUtils::$littleEndian)
$bytes = strrev($bytes);
$double = unpack("dflt", $bytes);
return $double['flt'];
}
btw: we also have an iPhone client send the double - works fine ...
iphone encoding:
(void)encodeDouble:(double)realv forKey:(NSString*)key;
{
if (key) [self writeTypedObject:key];
[self writeChar:'D'];
[self writeInt64:(int64_t)(*((double*)(&realv)))];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于 iOS 是小端字节序,我认为您希望在 Java 代码中以相反的顺序对
double
进行编码。Given that iOS is little-endian, I think you want to encode your
double
in the opposite order in the Java code.