0

I have such code in PHP:

$b1 = 0x80 | (0x1 & 0x0f);

$file = fopen('message.dat', 'w') or die('cannot create file');
fwrite($file, $b1);
fclose($file);

hexdump shows me:

$ hexdump message.dat 

0000000 3231 0039
0000003

The same code in Ruby:

b1 = 0x08 | (0x1 & 0x0f)
File.open('message.dat', 'w') { |file| file.write b1 }

hexdump shows me:

$ hexdump sockets/message.dat 

0000000 0039
0000001

b1 should be equal to 9 (decimal). Hex ASCII gives 39. Ok. But PHP gives extra 3231 hex.

Why?

PS. I want to convert PHP code to Ruby. This is WebSocket server. PHP code works properly. File output just only for debugging.

profport
  • 131
  • 1
  • 1
  • 7
  • "b1 should be equal to 9 (decimal)." What? No, not with the php code you've posted. `0x80 | (0x1 & 0x0f) = 0x81`. And that 129 in decimal. And if written in ascii (.e. the three characters `1`, `2` and `9`) that's the byte sequence `31 32 39`. – VolkerK May 14 '16 at 11:11
  • You have `0x08` in your Ruby code but `0x80` in your PHP code. Is that the problem? – cremno May 14 '16 at 11:42
  • 9 in case `b1 = 0x08 | (0x1 & 0x0f)` . Yes, `0x08` in Ruby and `0x80` in PHP. Oh, damned inattentiveness :) Thank you – profport May 14 '16 at 12:36

0 Answers0