So far, I am writing the bytes individually. That seems inefficient. Are there better alternatives, perhaps converting the vector to a byte string?
Output to where? A vector of UInt8 is a byte string
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> 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> 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> 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.
Ok, thanks. I don’t understand why I’m having problems, then. I’ll go back over my code.