# Convert vector of binaries to decimal and/or hex

**URL:** <https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974>\
**Category:** New to Julia\
**Tags:** numbers, convert\
**Created:** [January 20, 2025, 3:58pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974 "2025-01-20T15:58:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefaniecg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefaniecg/32/204815_2.png) [@stefaniecg](https://discourse.julialang.org/u/stefaniecg)\
**Post date:** [January 20, 2025, 3:58pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974/1 "2025-01-20T15:58:19Z")

</div>

I want to convert a binary string to decimal.

This converst one item of the array to an int value:

```julia
bit_vector = digits(0x0284, base=2, pad=16)
convert(Int,bit_vector[1])

```

but i want something like this, but it does not work.

```julia
convert(Int,bit_vector[1:4])
convert(UInt16,bit_vector[1:4])

```

Thank you for the help you can provide

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 20, 2025, 4:03pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974/2 "2025-01-20T16:03:27Z")

</div>

Can you clairfy what `bit_vector` is?

---

<div class="post-metadata">

**Author:** ![stefaniecg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefaniecg/32/204815_2.png) [@stefaniecg](https://discourse.julialang.org/u/stefaniecg)\
**Post date:** [January 20, 2025, 4:05pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974/3 "2025-01-20T16:05:26Z")

</div>

hi @mkitti,

`bit_vector` is the answer of executing `digits(0x0284, base=2, pad=16)`

if one does `typeof(bit_vector)` it is of type: `Vector{Int64} (alias for Array{Int64, 1})`

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [January 20, 2025, 4:14pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974/4 "2025-01-20T16:14:31Z")

</div>

Is `evalpoly` what you’re looking for?

```julia
julia> x = 0x0284
0x0284

julia> bit_vector = digits(x, base=2, pad=16)
16-element Vector{Int64}:
 0
 0
 1
 0
 0
 0
 0
 1
 0
 1
 0
 0
 0
 0
 0
 0

julia> evalpoly(2, bit_vector) == x
true

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 20, 2025, 4:15pm UTC](https://discourse.julialang.org/t/convert-vector-of-binaries-to-decimal-and-or-hex/124974/5 "2025-01-20T16:15:43Z")

</div>

```julia
julia> bit_vector = digits(0x0284, base=2, pad=16)
16-element Vector{Int64}:
 0
 0
 1
 0
 0
 0
 0
 1
 0
 1
 0
 0
 0
 0
 0
 0

julia> bit_vector .<< (0:15)
16-element Vector{Int64}:
   0
   0
   4
   0
   0
   0
   0
 128
   0
 512
   0
   0
   0
   0
   0
   0

julia> sum(bit_vector .<< (0:15))
644

julia> bits_to_int(bit_vector::Union{BitVector, Vector{Bool}}) =
          sum(bit_vector .<< (0:length(bit_vector)-1))
bits_to_int (generic function with 1 method)

julia> bits_to_int(Bool.(bit_vector))
644

julia> 0x0284 |> Int
644

```
