# Convert Vectors of Numbers and Bases to Two-Dimensional Matrix of Strings

**URL:** <https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520>\
**Category:** General Usage\
**Tags:** strings, numbers\
**Created:** [April 15, 2023, 7:04pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520 "2023-04-15T19:04:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cougar](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Cougar](https://discourse.julialang.org/u/Cougar)\
**Post date:** [April 15, 2023, 7:04pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/1 "2023-04-15T19:04:33Z")

</div>

* * *

Given two `Vector{Int64}` `n` and `b` shown below, I’m looking for a succinct and efficient way to build the `3×3 Matrix{String}` described …

* * *

- top row is binary, octal, hexadecimal expansions of 321.
- middle row is binary, octal, hexadecimal expansions of 111111.
- bottom row is binary, octal, hexadecimal expansions of 10000001.

* * *

```julia
  n::Vector{Int64} = [321, 111111, 10000001]
  b::Vector{Int64} = [2, 8, 16]
  #=
    Goal: using n and b, create ...
    3×3 Matrix{String}:
     "101000001" "501" "141"
     "11011001000000111" "331007" "1b207"
     "100110001001011010000001" "46113201" "989681"
  =#

```

* * *

If I were using Haskell, I’d consider currying something such as `f = ((n,b)->string(n,base=2))`. Not sure if such is possible or advisable in Julia.

* * *

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 15, 2023, 8:25pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/2 "2023-04-15T20:25:28Z")

</div>

is this enough?

```julia
julia> Base._base.(b',n,1, false)
3×3 Matrix{String}:
 "101000001" "501" "141"
 "11011001000000111" "331007" "1b207"
 "100110001001011010000001" "46113201" "989681"

```

I found [this](http://web.mit.edu/julia_v0.6.0/julia/share/doc/julia/html/en/stdlib/numbers.html) document, but I can’t get it to work without \_

```julia
julia> Base._base.(b',n,Int(ceil(log(2,maximum(n)))), false)
3×3 Matrix{String}:
 "000000000000000101000001" "000000000000000000000501" "000000000000000000000141"
 "000000011011001000000111" "000000000000000000331007" "00000000000000000001b207"
 "100110001001011010000001" "000000000000000046113201" "000000000000000000989681"

```

---

<div class="post-metadata">

**Author:** ![Cougar](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Cougar](https://discourse.julialang.org/u/Cougar)\
**Post date:** [April 15, 2023, 8:44pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/3 "2023-04-15T20:44:57Z")

</div>

The URL you reference includes `julia_v0.6.0` … is it of such an old version as to be out-of-date?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 15, 2023, 8:51pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/4 "2023-04-15T20:51:42Z")

</div>

Try:

```julia
julia> [join(digits(x; base=y)) for x in n, y in b]
3×3 Matrix{String}:
 "100000101" "105" "141"
 "11100000010011011" "700133" "702111"
 "100000010110100100011001" "10231164" "186989"

```

with `n` and `b` defined as in the previous posts in the thread. Note that it is always preferable not to use functions with `_` prefix, as they are considered internal to modules.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 15, 2023, 9:29pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/5 "2023-04-15T21:29:42Z")

</div>

Itried this way.  
but in the case of hexadecimal digits greater than 9, it does not get the required form.

b=\>11

and you also need to change the order

```julia

exa=(10:15) .=> 'a':'f'

[join(reverse(replace(digits(ni,base=bi),exa...))) for ni in n ,bi in b]

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 15, 2023, 10:21pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/6 "2023-04-15T22:21:31Z")

</div>

Thanks for the correction.  
Although the right way to use `digits` is awkward (and slower due to allocation), I still think it is better than `_base`.

So a better way would be:

```julia
julia> [string(x; base=y) for x in n, y in b]
3×3 Matrix{String}:
 "101000001" "501" "141"
 "11011001000000111" "331007" "1b207"
 "100110001001011010000001" "46113201" "989681"

```

(got this from searching source for `_base`)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 15, 2023, 10:29pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/7 "2023-04-15T22:29:30Z")

</div>

In order to be able to do `string.(n, b')`, is this piracy?

```julia
import Base:string
string(n,b) = string(n; base=b)
string.(n, b')

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 15, 2023, 10:34pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/8 "2023-04-15T22:34:05Z")

</div>

Use a different name, such as:

```julia
string_n_base(n,b) = string(n; base=b)
string_n_base.(n, b')

```

(and invent a new musical genre along the way)

---

<div class="post-metadata">

**Author:** ![Cougar](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Cougar](https://discourse.julialang.org/u/Cougar)\
**Post date:** [April 16, 2023, 12:54pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/9 "2023-04-16T12:54:13Z")

</div>

> [@Dan](#):
>
> `string_n_base.(n, b')`

This is the sort of thing that I sought. Thanks to all who replied.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 16, 2023, 2:39pm UTC](https://discourse.julialang.org/t/convert-vectors-of-numbers-and-bases-to-two-dimensional-matrix-of-strings/97520/10 "2023-04-16T14:39:40Z")

</div>

Thanks @Dan, the conductor, loved the musical analogy.
