I want to convert a binary string to decimal.
This converst one item of the array to an int value:
bit_vector = digits(0x0284, base=2, pad=16)
convert(Int,bit_vector[1])
but i want something like this, but it does not work.
convert(Int,bit_vector[1:4])
convert(UInt16,bit_vector[1:4])
Thank you for the help you can provide
mkitti
2
Can you clairfy what bit_vector
is?
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})
Is evalpoly
what you’re looking for?
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
2 Likes
mkitti
5
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