Writing 8-bit bytes

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.

It works fine for me. For example:

julia> a = rand(1024);

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

julia> filesize("tst.dat")

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.

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.

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

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