Advertisement
SplittyDev

Untitled

Jan 1st, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.77 KB | None | 0 0
  1. def print(val : Int)
  2.   hex = true
  3.   sign = val
  4.   arr = uninitialized UInt8[19]
  5.   accum = 0
  6.   if sign < 0
  7.     val = 0 - val
  8.   end
  9.   lut = StaticArray[
  10.     '0', '1', '2', '3',
  11.     '4', '5', '6', '7',
  12.     '8', '9', 'A', 'B',
  13.     'C', 'D', 'E', 'F'
  14.   ]
  15.   while true
  16.     arr[accum] = lut[(val.to_u % 16)].ord.to_u8
  17.     val = val.to_u / 16
  18.     accum += 1
  19.     break if val == 0
  20.   end
  21.   if hex
  22.     arr[accum + 0] = 'x'.ord.to_u8
  23.     arr[accum + 1] = '0'.ord.to_u8
  24.     accum += 2
  25.   end
  26.   if sign < 0
  27.     arr[accum] = '-'.ord.to_u8
  28.     accum += 1
  29.   end
  30.   arr[accum] = '\0'.ord.to_u8
  31.   i = 0
  32.   j = accum - 1
  33.   while i < j
  34.     c = arr[i]
  35.     arr[i] = arr[j]
  36.     arr[j] = c
  37.     i += 1
  38.     j -= 1
  39.   end
  40.   accum.times do |i|
  41.     Terminal.write_byte arr[i]
  42.   end
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement