def int_to_bytes(x, length):
  """Converts bigendian integer to byte array with fixed length."""
  res = bytearray(length)
  for i in range(length):
    res[length - i - 1] = int(x) % 256
    x = int(x) // 256
  return res