BitConverter.jl

BitConverter.jl

Converts base data types to an array of bytes, and an array of bytes to base data types. So far Integer only are implemented.

Functions

BitConverter.bytes โ€” Method.
bytes(x::Integer; len::Integer, little_endian::Bool)
-> Vector{len, UInt8}

Convert an Integer x to a Vector{UInt8} Options (not available for x::BigInt):

  • len to define a minimum Vector lenght in bytes, result will show no leading

zero by default.

  • set little_endian to true for a result in little endian byte order, result

in big endian order by default.

julia> bytes(32974)
2-element Array{UInt8,1}:
 0x80
 0xce

julia> bytes(32974, len=4)
4-element Array{UInt8,1}:
 0x00
 0x00
 0x80
 0xce

julia> bytes(32974, little_endian=true)
2-element Array{UInt8,1}:
 0xce
 0x80
BitConverter.to_big โ€” Method.
to_big(x::Vector{UInt8}) -> BigInt

Convert a Vector{UInt8} of any lenght to a BigInt. Considers the input a big endian.

julia> to_big([0x01, 0x00])
256
BitConverter.to_int โ€” Method.
Int(x::Vector{UInt8}; little_endian::Bool)
-> Integer

Convert a Vector{UInt8} an Integer, a BigInt above 64 bits Optionally set little_endian to true if the input as such byte order, input is considered big endian by default.

julia> Int([0x01, 0x00])
256

julia> Int([0x01, 0x00], little_endian=true)
1

Buy me a cup of coffee

Donate Bitcoin

Index