#!/usr/bin/env ruby

# 1608, Sun 23 Mar 08 (PDT)
# rlt-support.rb: Ruby routines to support ruby-libtrace
# Copyright (C) 2008, Nevil Brownlee, U Auckland | CAIDA | Wand


def s_to_hex(s,  offset)  # Print s as a hex dump
   os = "\n" + ''.rjust(offset)
   len = s.length
   o = '';  j = 0
   s.each_byte do |b|
      o << sprintf("%02x", b)
      j += 1
      if j != len
         o << ' ' if j%2 == 0
         o << ' ' if j%4 == 0
         o << os  if j%32 == 0
      end
   end
   o
end

def i_to_hex(i)  # return i as 4-hex-digit string
   lz = i <= 0xFFF ? '0' : ''
   lz << '0' if i <= 0xFF
   sprintf("#{lz}%x", i)
end

def get_short(a, x)  # Get value of two-byte fied from a[]
   return a[x].ord << 8 | a[x+1].ord
end

