Meaning of base and pad in string(n::Integer; base::Integer = 10, pad::Integer = 1)

Hi Gray! Welcome to the julia discourse.

Have you tried the documentation?

For example, if you write julia> ?string in the REPL and hit enter, you will get:

string(n::Integer; base::Integer = 10, pad::Integer = 1)

  Convert an integer n to a string in the given base, optionally specifying a number of digits to pad to.

  See also digits, bitstring, count_zeros.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> string(5, base = 13, pad = 4)
  "0005"

  julia> string(-13, base = 5, pad = 4)
  "-0023"

So let’s use an easy number: the 2. Remember that in binary (which is a base 2 number system) the number 2 is represented as “10”. Whereas in our typical numerical system (base 10) is represented as “2”.

julia> string(2,base=10,pad=10)
"0000000002"

julia> string(2,base=2,pad=10)
"0000000010"

So in both cases, pad=10 tells julia to return a string of 10 characters. If needed, julia will “pad” with zeros to the left of the original number.

In the first case you are getting the representation of 2 in our base 10 system. In the second one you are getting it in binary (base 2). This is accomplished by the argument “base”.

8 Likes