# Writing 8-bit bytes

**URL:** https://discourse.julialang.org/t/writing-8-bit-bytes/15334
**Category:** General Usage
**Created:** [September 22, 2018, 10:57am UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334 "2018-09-22T10:57:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Feldman](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@David\_Feldman](https://discourse.julialang.org/u/David_Feldman)
#### Post date: [September 22, 2018, 10:57am UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/1 "2018-09-22T10:57:53Z")

</div>

I wanted to create a certain file consisting of 8-bit bytes (to conform with a picture format).

In the following, the value of Q was always in the range 0-255. But the command  
write(OUT, convert(UInt8,floor(Q)))  
actually wrote zero-padded 16-bit bytes to the file OUT. Thus output file came out twice the size I expected. Indeed, using another programming language, I got the desired result by “dealing” 8-bit bytes (like card) between to two files (the other of which got filled with 0’s and thus subsequently discarded).

How can I write 8-bit bytes in Julia? I’m using Windows 10 on a 64-bit machine, if that makes a difference.

Thank you.

---

<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: [September 22, 2018, 12:15pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/2 "2018-09-22T12:15:12Z")

</div>

It works fine for me. For example:

```julia
julia> a = rand(1024);

julia> open("tst.dat", "w") do f
           for x in a
               write(f, convert(UInt8, floor(x)))
           end
       end

julia> filesize("tst.dat")
1024

```

---

<div class="post-metadata">

### Author: ![David\_Feldman](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@David\_Feldman](https://discourse.julialang.org/u/David_Feldman)
#### Post date: [September 22, 2018, 7:16pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/3 "2018-09-22T19:16:26Z")

</div>

Either “weird.”  
Or…perhaps filesize is counting 16-bit blocks?  
That would be consistent. I’ll try filesize with my code and see what it reports.

---

<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: [September 22, 2018, 7:18pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/4 "2018-09-22T19:18:04Z")

</div>

filesize returns the size in bytes (8 bits).

Probably you have some mistake, but it is hard to tell what is going on if you don’t provide a self-contained example like what I posted.

---

<div class="post-metadata">

### Author: ![David\_Feldman](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@David\_Feldman](https://discourse.julialang.org/u/David_Feldman)
#### Post date: [September 22, 2018, 7:53pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/5 "2018-09-22T19:53:16Z")

</div>

Agreed.  
I can reproduce your example. I’ll look for my mistake now.

---

<div class="post-metadata">

### Author: ![David\_Feldman](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@David\_Feldman](https://discourse.julialang.org/u/David_Feldman)
#### Post date: [September 22, 2018, 7:55pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/6 "2018-09-22T19:55:54Z")

</div>

Found it. Thanks… Probably this topic should be deleted.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [September 22, 2018, 8:07pm UTC](https://discourse.julialang.org/t/writing-8-bit-bytes/15334/7 "2018-09-22T20:07:33Z")

</div>


