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

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.

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

is this enough?

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

I found this document, but I can’t get it to work without _

``````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"
``````

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

Try:

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

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

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

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

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> [ 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`)

3 Likes

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

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

Use a different name, such as:

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

(and invent a new musical genre along the way)

1 Like

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

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