teeworlds_network/lib/bytes.rb

20 lines
392 B
Ruby
Raw Normal View History

2022-11-05 16:48:47 +00:00
# frozen_string_literal: true
# turn byte array into hex string
def str_hex(data)
2022-11-05 16:19:05 +00:00
data.unpack1('H*').scan(/../).join(' ').upcase
end
# turn hex string to byte array
def str_bytes(str)
2022-11-05 16:19:05 +00:00
str.scan(/../).map { |b| b.to_i(16) }
end
def bytes_to_str(data)
2022-11-05 16:19:05 +00:00
data.unpack('H*').join('')
end
def get_byte(data, start = 0, num = 1)
2022-11-05 16:19:05 +00:00
data[start...(start + num)].unpack('H*').join('').upcase
end