# Write bytes of object to io

**URL:** https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444
**Category:** General Usage
**Created:** [January 9, 2019, 6:36pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444 "2019-01-09T18:36:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [January 9, 2019, 6:36pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/1 "2019-01-09T18:36:27Z")

</div>

I can read a struct from its bytes from an `IO` like so:

```julia
julia> struct MyInt; data::Int; end

julia> io = IOBuffer(); write(io, 42); seekstart(io)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=8, maxsize=Inf, ptr=1, mark=-1)

julia> read!(io, Ref(MyInt(-999)))
Base.RefValue{MyInt}(MyInt(42))

```

How to do the inverse, e.g. write the bytes of a struct to an `io`? The obvious thing fails:

```julia

julia> write(io, MyInt(42))
ERROR: MethodError: no method matching write(::Base.GenericIOBuffer{Array{UInt8,1}}, ::MyInt)
Closest candidates are:
  write(::IO, ::Any) at io.jl:498
  write(::IO, ::Any, ::Any...) at io.jl:500
  write(::IO, ::Complex) at complex.jl:217
  ...
Stacktrace:
 [1] write(::Base.GenericIOBuffer{Array{UInt8,1}}, ::MyInt) at ./io.jl:498
 [2] top-level scope at none:0

```

Also I am confused by the error. It seems that the first listed signature `write(::IO, ::Any) at io.jl:498` applies?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [January 9, 2019, 6:55pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/2 "2019-01-09T18:55:34Z")

</div>

One way to do this is to reinterpret the struct as bytes (i.e. `UInt8`) and write those:

```julia
julia> write(io, reinterpret(UInt8, [MyInt(42)]))

```

I suspect this will be fairly inefficient, though, as allocating the `Vector{MyInt}` seems wasteful. Perhaps there is a better way.

Edit: See @yuyichao’s better solution below.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 9, 2019, 7:06pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/3 "2019-01-09T19:06:36Z")

</div>

```julia
julia> write(io, Ref(MyInt(-999)))
8

```

---

<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: [January 9, 2019, 7:07pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/4 "2019-01-09T19:07:32Z")

</div>

`write(io, Ref(MyInt(42)))` works. (Under the hood, it calls `unsafe_write` to output the raw bytes of a `struct`.)

(However, realize that this kind of binary I/O does not result in portable files in general, because of endian-ness, `sizeof(Int)` difference between 32-bit and 64-bit machines, and potentially other factors.)

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [January 9, 2019, 8:10pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/5 "2019-01-09T20:10:05Z")

</div>

Can you comment, why I have to wrap my type with a `Ref`, while some other types like `Int` do not need to be wrapped?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 9, 2019, 8:23pm UTC](https://discourse.julialang.org/t/write-bytes-of-object-to-io/19444/6 "2019-01-09T20:23:10Z")

</div>

You can wrap them too. There’s no harm to allow no wrapping when there isn’t ambiguity so the few builtin types allows this. In general your type may want a different behavior so that’s not there by default. No one stops you from implementing it for your type though if that’s the only way your type should be written.
