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

**URL:** https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798
**Category:** New to Julia
**Tags:** strings
**Created:** [November 5, 2022, 2:05am UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798 "2022-11-05T02:05:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Gray](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gray/32/44821_2.png) [@Gray](https://discourse.julialang.org/u/Gray)
#### Post date: [November 5, 2022, 2:05am UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/1 "2022-11-05T02:05:42Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [November 5, 2022, 2:18am UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/2 "2022-11-05T02:18:56Z")

</div>

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:

```julia
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)](https://en.wikipedia.org/wiki/Binary_number) the number 2 is represented as “10”. Whereas in [our typical numerical system (base 10)](https://en.wikipedia.org/wiki/Decimal) is represented as “2”.

```julia
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”.

---

<div class="post-metadata">

### Author: ![Gray](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gray/32/44821_2.png) [@Gray](https://discourse.julialang.org/u/Gray)
#### Post date: [November 5, 2022, 9:31am UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/3 "2022-11-05T09:31:26Z")

</div>

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.

Thanks again for your explanation.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [November 5, 2022, 4:56pm UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/4 "2022-11-05T16:56:12Z")

</div>

> [@Gray](#):
>
> how many different bases there are and when they are used

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.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [November 5, 2022, 5:25pm UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/5 "2022-11-05T17:25:26Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [November 5, 2022, 5:40pm UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/6 "2022-11-05T17:40:16Z")

</div>

> [@Gray](#):
>
> The only differences I see are when I use the binary. Everything else seems to be base 10.

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

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [November 5, 2022, 11:27pm UTC](https://discourse.julialang.org/t/meaning-of-base-and-pad-in-string-n-integer-base-integer-10-pad-integer-1/89798/7 "2022-11-05T23:27:43Z")

</div>

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… ([![Babylonian 1.svg](https://global.discourse-cdn.com/julialang/original/3X/0/2/021a689e6ff25a627d3533bb86f6319f1ae39bd9.png)](https://en.wikipedia.org/wiki/File:Babylonian_1.svg), [![Babylonian 2.svg](https://global.discourse-cdn.com/julialang/original/3X/5/d/5debbd45fee5c400db0267ad004bab2f406aa412.png)](https://en.wikipedia.org/wiki/File:Babylonian_2.svg), [![Babylonian 3.svg](https://global.discourse-cdn.com/julialang/original/3X/3/1/31a4ef9338d655e5a1a725e2f348768ad20756fd.png)](https://en.wikipedia.org/wiki/File:Babylonian_3.svg), [![Babylonian 4.svg](https://global.discourse-cdn.com/julialang/original/3X/0/8/087ca24d22417faeb81aa436a44adc514ea73749.png)](https://en.wikipedia.org/wiki/File:Babylonian_4.svg), …, it would be a cool option, though an easy transformation, done elsewhere).

Intriguingly only works up to:

```julia
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`](https://docs.julialang.org/en/v1/stdlib/Base64/#Base64.base64encode) and [`Base64.base64decode`](https://docs.julialang.org/en/v1/stdlib/Base64/#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):

> **[RFC 9285: The Base45 Data Encoding](https://datatracker.ietf.org/doc/rfc9285/)**
>
> This document describes the Base45 encoding scheme, which is built upon the Base64, Base32, and Base16 encoding schemes.

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

> In version 1.1[[17]](https://en.wikipedia.org/wiki/Sexagesimal#cite_note-17) of the [YAML](https://en.wikipedia.org/wiki/YAML) data storage format, sexagesimals are supported for plain scalars, and formally specified both for integers[[18]](https://en.wikipedia.org/wiki/Sexagesimal#cite_note-18) and floating point numbers.[[19]](https://en.wikipedia.org/wiki/Sexagesimal#cite_note-19) This has led to confusion, as e.g. some [MAC addresses](https://en.wikipedia.org/wiki/MAC_address) 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]](https://en.wikipedia.org/wiki/Sexagesimal#cite_note-20)
