Fixpoint pos_length (p : positive) : nat :=
  match p with
  | xH => 0
  | xO p' => S (pos_length p')
  | xI p' => S (pos_length p')
  end.

Theorem digitInv : forall (q : Z) (r : nat), (Zpower_nat n (pred r) <= Zabs q)%Z -> (Zabs q < Zpower_nat n r)%Z -> digit q = r.