Skip to content
Browse files

Merge pull request #38 from OtherLevels/issue_36

fixed signed 16bit amqp framing conversion
  • Loading branch information...
2 parents 1da9615 + 3432bcb commit 0c6dfc14abb3c4a3173efe00783a971f4021780f @michaelklishin michaelklishin committed Dec 18, 2013
Showing with 23 additions and 4 deletions.
  1. +2 −0 .gitignore
  2. +4 −3 lib/amq/pack.rb
  3. +17 −1 spec/amq/{hacks_spec.rb → pack_spec.rb}
View
2 .gitignore
@@ -3,6 +3,8 @@
/.ruby-version
tmp
*.pyc
+*.iml
+.idea
/vendor/bundle
/vendor/amq-*
/coverage
View
7 lib/amq/pack.rb
@@ -7,7 +7,8 @@ module AMQ
# compatible with Ruby 1.8+.
module Pack
UINT64 = "Q".freeze
- INT16 = "c".freeze
+ UINT16_BE = "n".freeze
+ INT16 = "c".freeze
if Endianness.big_endian?
def self.pack_uint64_big_endian(long_long)
@@ -42,8 +43,8 @@ def self.pack_int16_big_endian(short)
end
def self.unpack_int16_big_endian(data)
- data = data.bytes.to_a.reverse.map(&:chr).join
- data.unpack(INT16)
+ value = data.bytes.to_a.map(&:chr).join.unpack(UINT16_BE)[0]
+ [(value & ~(1 << 15)) - (value & (1 << 15))]
end
end
end
View
18 spec/amq/hacks_spec.rb → spec/amq/pack_spec.rb
@@ -4,7 +4,23 @@
module AMQ
- describe Hacks do
+ describe Pack do
+ context "16-bit big-endian packing / unpacking" do
+ let(:examples_16bit) {
+ {
+ 0x068D => "\x06\x8D" # 1677
+ }
+ }
+
+ it "unpacks signed integers from a string to a number" do
+ examples_16bit.each do |key, value|
+ described_class.unpack_int16_big_endian(value)[0].should == key
+ end
+ end
+ end
+
+
+
context "64-bit big-endian packing / unpacking" do
let(:examples) {
{

0 comments on commit 0c6dfc1

Please sign in to comment.
Something went wrong with that request. Please try again.