Module: RubyCraft::ByteConverter

Extended by:
ByteConverter
Included in:
ByteConverter, LazyChunkDelegate, NbtHelper, Region
Defined in:
lib/rubycraft/byte_converter.rb

Overview

Utils for manipulating bytes back and forth as strings, strings and numbers

Instance Method Summary collapse

Instance Method Details

#arrayToIO(arr) ⇒ Object



18
19
20
21
22
23
# File 'lib/rubycraft/byte_converter.rb', line 18

def arrayToIO(arr)
  io = StringIO.new
  io.write toByteString arr
  io.rewind
  io
end

#bytesToInt(array) ⇒ Object



35
36
37
# File 'lib/rubycraft/byte_converter.rb', line 35

def bytesToInt(array)
  array.pack('C*').unpack("N").first
end

#concat(array, enum) ⇒ Object



29
30
31
32
33
# File 'lib/rubycraft/byte_converter.rb', line 29

def concat(array, enum)
  for i in enum
    array << i
  end
end

#intBytes(i) ⇒ Object



10
11
12
# File 'lib/rubycraft/byte_converter.rb', line 10

def intBytes(i)
  [i >> 24, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF]
end

#pad(array, count, value = 0) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubycraft/byte_converter.rb', line 39

def pad(array, count, value = 0)
  count.times do
    array << value
  end
  array
end

#stringToByteArray(str) ⇒ Object



14
15
16
# File 'lib/rubycraft/byte_converter.rb', line 14

def stringToByteArray(str)
  str.bytes.to_a
end

#stringToIo(str) ⇒ Object



25
26
27
# File 'lib/rubycraft/byte_converter.rb', line 25

def stringToIo(str)
  arrayToIO stringToByteArray(str)
end

#toByteString(array) ⇒ Object



6
7
8
# File 'lib/rubycraft/byte_converter.rb', line 6

def toByteString(array)
  array.pack('C*')
end