dongwei7048
dongwei7048
2012-05-02 18:24

从PHP中的Java DataOutputStream读取二进制数据时的整数格式是什么?

  • java
  • php
  • file-io

I'm aware that this is probably not the best idea but I've been playing around trying to read a file in PHP that was encoded using Java's DataOutputStream.

Specifically, in Java I use:

dataOutputStream.writeInt(number);

Then in PHP I read the file using:

$data = fread($handle, 4);
$number = unpack('N', $data);

The strange thing is that the only format character in PHP that gives the correct value is 'N', which is supposed to represent "unsigned long (always 32 bit, big endian byte order)". I thought that int in java was always signed?

Is it possible to reliably read data encoded in Java in this way or not? In this case the integer will only ever need to be positive. It may also need to be quite large so writeShort() is not possible. Otherwise of course I could use XML or JSON or something.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答