Integer to string in any base

20-minute lesson · module-recursion-03

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Integer to string in any base

20-minute lesson · module-recursion-03

  • Express base conversion as a recursive divide-and-remainder process.
  • Map a remainder to its digit character for bases 2 through 16.
  • Identify the base case (value smaller than the base = a single digit).
  • Order the recursion so the most significant digit comes first.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Recursion / Converting an Integer to a String in Any Base

Adaptation notes. Reading rewritten in our voice; the digit-string lookup and the divide/remainder recursion follow the chapter's classic approach but the prose and trace are ours. Hidden tests cover binary, octal, decimal, and hexadecimal including the letter digits A–F and the zero edge case.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.