Hi,

Yes, reinterpret is the kind of command I was searching for. However,
reinterpret(Bool, b"abc") returns an Array{Bool,1}
of size 3 ( [true, true, true]) instead of the BitVector of size 24 I would
like.

I've wrote this little function to do the staff, but it's a bit creepy :
create a BitVector and after alter the field "chunk".
function bytesToBitVector(s::Array{Uint8,1})
       n = size(s,1)
       padding = 8-n%8
       chunks = reinterpret(Uint64, [s, repmat(b"\0",padding)])
       t = BitVector(n*8)
       t.chunks = chunks
       t
end

Sébastien


2014-11-29 21:01 GMT+01:00 Mauro <[email protected]>:

> are you looking for:
>
> help?> reinterpret
> Base.reinterpret(type, A)
>
>    Change the type-interpretation of a block of memory. For example,
>    "reinterpret(Float32, uint32(7))" interprets the 4 bytes
>    corresponding to "uint32(7)" as a "Float32". For arrays, this
>    constructs an array with the same binary data as the given array,
>    but with the specified element type.
>
>
>
> On Sat, 2014-11-29 at 15:11, [email protected] wrote:
> > Hello,
> >
> > I would like to convert a byte string like b"abe34" into a BitArray. I
> > could use this function, but it will be horribly slow :
> >
> > function bytesToBitarray(s::Array{Uint8,1})
> >        t = BitArray(size(s, 1)*8)
> >        for i in 1:size(s, 1)
> >            for j in 0:7
> >              t[8(i-1) + j + 1] = s[i] & (1<<j)
> >            end
> >        end
> >        t
> >  end
> >
> >  how can I do this efficiently ?
> >
> > Sébastien
>
>

Reply via email to