CANalyze.Utils
CANalyze.Utils
— ModuleThe module provides utilities to convert numbers into and from byte representations, functions to check whether the system is little-endian or big-endian, and functions to create bitmasks.
CANalyze.Utils.bit_mask
— Methodbit_mask(::Type{T}, bits::Set{UInt16}) where {T <: Integer} -> T
Creates a bit mask of type T
where every bit inside bits
is set.
Arguments
Type{T}
: the type of the maskbits::Set{UInt16}
: the set of bits we want to set
Returns
T
: the mask
Examples
using CANalyze.Utils
m = Utils.bit_mask(UInt8, Set{UInt16}([0,1,2,3,4,5,6,7]))
# output
0xff
CANalyze.Utils.from_bytes
— Methodfrom_bytes(type::Type{T}, array::AbstractArray{UInt8}) where {T <: Number} -> T
Creates a value of type T
constituted by the byte-array array
. If the array
length is smaller than the size of T
, array
is filled with enough zeros.
Arguments
type::Type{T}
: the type to which the byte-array is transformedarray::AbstractArray{UInt8}
: the byte array
Returns
T
: the value constructed from the byte sequence
Examples
using CANalyze.Utils
bytes = Utils.from_bytes(UInt16, UInt8[0xFF, 0xAA])
# output
0xaaff
CANalyze.Utils.full_mask
— Methodfull_mask(::Type{T}) where {T <: Integer} -> T
Creates a full mask of type T
with 8sizeof(T)
bits.
Arguments
Type{T}
: the type of the mask
Returns
T
: the full mask
Examples
using CANalyze.Utils
m = Utils.full_mask(Int8)
# output
-1
CANalyze.Utils.is_big_endian
— Methodis_big_endian() -> Bool
Returns whether the system has big-endian byte-order
Returns
Bool
: The system has big-endian byte-order
CANalyze.Utils.is_little_endian
— Methodis_little_endian() -> Bool
Returns whether the system has little-endian byte-order
Returns
Bool
: The system has little-endian byte-order
CANalyze.Utils.mask
— Methodmask(::Type{T}) where {T <: Integer} -> T
Creates a full mask of type T
with 8sizeof(T)
bits.
Arguments
Type{T}
: the type of the mask
Returns
T
: the full mask
Examples
using CANalyze.Utils
m = Utils.mask(UInt64)
# output
0xffffffffffffffff
CANalyze.Utils.mask
— Methodmask(::Type{T}, length::Integer, shift::Integer) where {T <: Integer} -> T
Creates a mask of type T
with length
number of bits and right-shifted by shift
number of bits.
Arguments
Type{T}
: the type of the masklength::Integer
: the number of bitsshift::Integer
: the right-shift
Returns
T
: the mask defined bylength
andshift
Examples
using CANalyze.Utils
m = Utils.mask(UInt64, 32, 16)
# output
0x0000ffffffff0000
CANalyze.Utils.mask
— Methodmask(::Type{T}, length::Integer) where {T <: Integer} -> T
Creates a mask of type T
with length
number of bits.
Arguments
Type{T}
: the type of the masklength::Integer
: the number of bits
Returns
T
: the mask defined bylength
Examples
using CANalyze.Utils
m = Utils.mask(UInt64, 32)
# output
0x00000000ffffffff
CANalyze.Utils.mask
— Methodmask(::Type{T}, length::UInt8, shift::UInt8) where {T <: Integer} -> T
Creates a mask of type T
with length
number of bits and right-shifted by shift
number of bits.
Arguments
Type{T}
: the type of the masklength::UInt8
: the number of bitsshift::UInt8
: the right-shift
Returns
T
: the mask defined bylength
andshift
CANalyze.Utils.mask
— Methodmask(::Type{T}, length::UInt8) where {T <: Integer} -> T
Creates a mask of type T
with length
number of bits.
Arguments
Type{T}
: the type of the masklength::UInt8
: the number of bits
Returns
T
: the mask defined bylength
CANalyze.Utils.to_bytes
— Methodto_bytes(num::Number) -> Vector{UInt8}
Creates the byte representation of the number num
.
Arguments
num::Number
: the number from which we retrieve the bytes.
Returns
Vector{UInt8}
: the bytes representation of the numbernum
Examples
using CANalyze.Utils
bytes = Utils.to_bytes(UInt16(0xAAFF))
# output
2-element Vector{UInt8}:
0xff
0xaa
CANalyze.Utils.zero_mask
— Methodzero_mask(::Type{T}) where {T <: Integer} -> T
Creates a zero mask of type T
where every bit is unset.
Arguments
Type{T}
: the type of the mask
Returns
T
: the zero mask
Examples
using CANalyze.Utils
m = Utils.zero_mask(UInt8)
# output
0x00