# An old question: how do I output a vector of UInt8 in binary?

**URL:** https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016
**Category:** New to Julia
**Created:** [December 1, 2023, 8:23pm UTC](https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016 "2023-12-01T20:23:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rmfritz](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.julialang.org/u/rmfritz)
#### Post date: [December 1, 2023, 8:23pm UTC](https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016/1 "2023-12-01T20:23:14Z")

</div>

So far, I am writing the bytes individually. That seems inefficient. Are there better alternatives, perhaps converting the vector to a byte string?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [December 1, 2023, 9:15pm UTC](https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016/2 "2023-12-01T21:15:04Z")

</div>

Output to where? A vector of UInt8 is a byte string

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 1, 2023, 9:57pm UTC](https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016/3 "2023-12-01T21:57:03Z")

</div>

A vector of UInt8 is _already_ in memory as binary. Julia _displays_ it to humans in hexadecimal representation by default. Pretty much any way you look at it, you’ll just see a vector of UInt8. Do you want to write those bytes to a file? That’s a `write`:

```Julia-repl
julia> write("file.bin", [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a])
6

shell> xxd file.bin
00000000: 6865 6c6c 6f0a hello.

```

Do you want to see the literal ones and zeros? Julia can show those to you as a string of `'0'`s and `'1'`s with `bitstring`:

```julia-repl
julia> bitstring.([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a])
6-element Vector{String}:
 "01101000"
 "01100101"
 "01101100"
 "01101100"
 "01101111"
 "00001010"

```

Or you can even print it out as the Julia syntax to reconstruct the very same vector, using binary integer literals with the `0b` syntax of 0s and 1s:

```Julia-repl
julia> println("[", join(string.("0b", bitstring.([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a])), ", "), "]")
[0b01101000, 0b01100101, 0b01101100, 0b01101100, 0b01101111, 0b00001010]

julia> show([0b01101000, 0b01100101, 0b01101100, 0b01101100, 0b01101111, 0b00001010])
UInt8[0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a]

```

These are all just different ways of interpreting (or displaying) the same bit pattern. The bit pattern `01101000` can be interpreted as hexadecimal (`0x68`) or decimal (`104`) or even the ASCII `'h'`… or anything, really.

---

<div class="post-metadata">

### Author: ![rmfritz](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.julialang.org/u/rmfritz)
#### Post date: [December 1, 2023, 10:18pm UTC](https://discourse.julialang.org/t/an-old-question-how-do-i-output-a-vector-of-uint8-in-binary/107016/4 "2023-12-01T22:18:15Z")

</div>

Ok, thanks. I don’t understand why I’m having problems, then. I’ll go back over my code.
