# Equivalent of numpy.tobytes and numpy.frombuffer in Julia

**URL:** <https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554>\
**Category:** General Usage\
**Tags:** question, array, memory\
**Created:** [April 4, 2026, 12:51pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554 "2026-04-04T12:51:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilachint](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@lilachint](https://discourse.julialang.org/u/lilachint)\
**Post date:** [April 4, 2026, 12:51pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554/1 "2026-04-04T12:51:48Z")

</div>

Hi! As the topic stated, I once built a [Python package](https://github.com/wavim/gradupe) that uses [`numpy.tobytes`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html) ([source](https://github.com/wavim/gradupe/blob/17d69779d9e6cc84a05205999c140a0d51b55d50/src/gradupe/cli.py#L18)) and [`numpy.frombuffer`](https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html) ([source](https://github.com/wavim/gradupe/blob/17d69779d9e6cc84a05205999c140a0d51b55d50/src/gradupe/cli.py#L119)) when caching things with SQLite.

In Julia, I want to do this with BitArrays instead to save even more space, but cannot find suitable methods to do so. We do have `Serialization.serialize` and JLD2.jl, but they don’t do quite the same thing. The numpy functions work specifically on arrays and allow us to specify the dtype, eliminating the need of serialization headers. The Julia methods doesn’t provide this convenience.

Are there standard library functions or specific packages I missed that could replace the numpy functions? Or would I have to write the functions myself? Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 4, 2026, 1:50pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554/2 "2026-04-04T13:50:59Z")

</div>

If you have an ordinary `Array` of a bitstype, which is analogous to a numpy array, you can use `reinterpret`:

```julia-auto
julia> a = rand(3)
3-element Vector{Float64}:
 0.3520259358052087
 0.3930310871507644
 0.16629023329978043

julia> reinterpret(UInt8, a)
24-element reinterpret(UInt8, ::Vector{Float64}):
 0x80
 0x23
 0x68
 0xca
 0x97
 0x87
 0xd6
 0x3f
 0x4e
 0xed
 0x67
 0xdc
 0x6b
 0x27
 0xd9
 0x3f
 0xa0
 0x58
 0xd5
 0x94
 0xff
 0x48
 0xc5
 0x3f

```

But you can also just call `write(io, array)` to write the raw bytes without calling `reinterpret`, so I’m a little confused about what you are trying to do.

> [@lilachint](#):
>
> In Julia, I want to do this with BitArrays instead to save even more space,

`reinterpret` won’t do what you want with a `BitArray`. The raw storage bytes (which are the bits packed into 64-bit chunks are in `somebitarray.chunks`:

```julia-auto
julia> a = BitArray(rand(Bool, 100));

julia> a.chunks
2-element Vector{UInt64}:
 0x2faeb60b28afdceb
 0x00000006a2d7843a

```

> [@lilachint](#):
>
> We do have `Serialization.serialize` and [JLD2.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/JLD2), but they don’t do quite the same thing.

Aside from serialization headers, the `Serialization` standard library directly writes the bytes of `a.chunks`.

You can also directly call `write(io, somebitarray)` and `read!(io, somebitarray)`, since [`BitArray` defines specialized `write` and `read!` methods](https://github.com/JuliaLang/julia/blob/dd6fee6e6dc680b3882238798f20a5dd53c9bd08/base/bitarray.jl#L1910-L1922) that write/read the raw `chunks`. That seems more like what you want?

(Note also that you can use an `IOBuffer` to write/read to/from an array of bytes rather than a file.)

---

<div class="post-metadata">

**Author:** ![lilachint](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@lilachint](https://discourse.julialang.org/u/lilachint)\
**Post date:** [April 4, 2026, 2:17pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554/3 "2026-04-04T14:17:16Z")

</div>

I knew about `reinterpret` and `BitArray.chunks`, but I finally figured that probably the actual problem in my case is that Julia doesn’t seem have a `bytes` type, which is built into Python (is IOBuffer similar to that?). And I don’t like the idea of storing the results as a BigInt either. To make things clearer, what I wanted to do is storing raw bytes from `BitVector.chunks` without header (which would be too large since the arrays themselves I’m using wouldn’t be large) as SQLite values, and be able to read that back into a `BitVector`. JLD2.jl could technically replace SQLite, but the gigantic overhead of metadata is unbearable in my case.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 4, 2026, 4:01pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554/4 "2026-04-04T16:01:08Z")

</div>

> [@lilachint](#):
>
> the actual problem in my case is that Julia doesn’t seem have a `bytes` type,

The analogue is just `Vector{UInt8}` (though this is mutable so it is closer to `bytearray` in Python). (`IOBuffer` is a wrapper around this that you can `read` or `write` with.)

> [@lilachint](#):
>
> And I don’t like the idea of storing the results as a BigInt either.

`BigInt` is totally the wrong type for storing arbitrary byte sequences.

> [@lilachint](#):
>
> To make things clearer, what I wanted to do is storing raw bytes from `BitVector.chunks` without header (which would be too large since the arrays themselves I’m using wouldn’t be large) as SQLite values, and be able to read that back into a `BitVector`

```julia-auto
julia> a = BitVector([true, false, true, true, false, true, true, true]);

julia> buf = IOBuffer();

julia> write(buf, a);

julia> bytes = take!(buf)
8-element Vector{UInt8}:
 0xed
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00

julia> b = BitVector(undef, 8);

julia> read!(IOBuffer(bytes), b);

julia> b
8-element BitVector:
 1
 0
 1
 1
 0
 1
 1
 1

julia> b == a
true

```

---

<div class="post-metadata">

**Author:** ![lilachint](https://avatars.discourse-cdn.com/v4/letter/l/ee7513/32.png) [@lilachint](https://discourse.julialang.org/u/lilachint)\
**Post date:** [April 4, 2026, 4:05pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-tobytes-and-numpy-frombuffer-in-julia/136554/5 "2026-04-04T16:05:48Z")

</div>

Thank you! I would look into this and SQLite.jl then.
