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

Hello, I understand how to use strings, but it’s not clear what exactly the base and pad are doing here:

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

Can anyone help explain what operation they are performing?

Thanks!

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.

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"

"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”.

7 Likes

Thank you very much for your explanation aramirezreyes. I understand the pad now, and the binary vs base 10 system makes sense.

I’m curious about how many different bases there are and when they are used. The only differences I see are when I use the binary. Everything else seems to be base 10.

Bases 2, 8, 10, and 16 are common. 8 is called octal and often exposed in file system permissions.
16 is called hexadecimal and is a convenient representation for individual byte values. Another commonly used for text encoding is 64, as in base64 encoding. There are also other bases used for text encoding.

1 Like

I also want to mention dozenal (base `12_X`). It was historically common, and there’s a bit of a movement to use it again. And hexagesimal (`60_X`) is famously important in history. I don’t know how `string` handles these cases.

1 Like

You need to try a number larger or equal than the base to see the difference. That is why 2 is represented as “2” if you set base=n with n larger than 2:

``````julia> for num = 2:4
for base = 2:5
@info "Num: ", num, " Base: ", base, " result: ", string(num,base=base)
end
end
[ Info: ("Num: ", 2, " Base: ", 2, " result: ", "10")
[ Info: ("Num: ", 2, " Base: ", 3, " result: ", "2")
[ Info: ("Num: ", 2, " Base: ", 4, " result: ", "2")
[ Info: ("Num: ", 2, " Base: ", 5, " result: ", "2")
[ Info: ("Num: ", 3, " Base: ", 2, " result: ", "11")
[ Info: ("Num: ", 3, " Base: ", 3, " result: ", "10")
[ Info: ("Num: ", 3, " Base: ", 4, " result: ", "3")
[ Info: ("Num: ", 3, " Base: ", 5, " result: ", "3")
[ Info: ("Num: ", 4, " Base: ", 2, " result: ", "100")
[ Info: ("Num: ", 4, " Base: ", 3, " result: ", "11")
[ Info: ("Num: ", 4, " Base: ", 4, " result: ", "10")
[ Info: ("Num: ", 4, " Base: ", 5, " result: ", "4")
``````

So if you see, only when the base is equal or smaller that the number you are printing, you will see a difference in the representation.

1 Like

Dozenal, base-12 works (though not its fractions), aka duodecimal or uncial, and hexagesimal, base-60, aka sexagesimal (though not with this Babylonian form of numbers… (, , , , …, it would be a cool option, though an easy transformation, done elsewhere).

Intriguingly only works up to:

``````julia> string(-1121212, base=62)  # base 2, 8, 10 and 16 are special-cased, as common
"-4hg4"
``````

likely because base-64 isn’t straight-forward, running out of the English/Latin alphabet (also other issues with many it and bases), and `Base64.base64encode` and `Base64.base64decode` available for that.

There are many straightforward to en/decode such as base-32, z-base-32, and there are also Ascii85, yEnc, Base122, BaseXML, basE91, and Draft IEFT for QR codes (you get a different encoding with base=45 in Julia):

I was surprised at seeing the “modern usage” (then dropped) for base-64:

In version 1.1[17] of the YAML data storage format, sexagesimals are supported for plain scalars, and formally specified both for integers[18] and floating point numbers.[19] This has led to confusion, as e.g. some MAC addresses would be recognised as sexagesimals and loaded as integers, where others were not and loaded as strings. In YAML 1.2 support for sexagesimals was dropped.[20]

1 Like