CANalyze.Utils

CANalyze.UtilsModule

The 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_maskMethod
bit_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 mask
  • bits::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_bytesMethod
from_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 transformed
  • array::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_maskMethod
full_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_endianMethod
is_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_endianMethod
is_little_endian() -> Bool

Returns whether the system has little-endian byte-order

Returns

  • Bool: The system has little-endian byte-order
CANalyze.Utils.maskMethod
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.mask(UInt64)

# output
0xffffffffffffffff
CANalyze.Utils.maskMethod
mask(::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 mask
  • length::Integer: the number of bits
  • shift::Integer: the right-shift

Returns

  • T: the mask defined by length and shift

Examples

using CANalyze.Utils
m = Utils.mask(UInt64, 32, 16)

# output
0x0000ffffffff0000
CANalyze.Utils.maskMethod
mask(::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 mask
  • length::Integer: the number of bits

Returns

  • T: the mask defined by length

Examples

using CANalyze.Utils
m = Utils.mask(UInt64, 32)

# output
0x00000000ffffffff
CANalyze.Utils.maskMethod
mask(::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 mask
  • length::UInt8: the number of bits
  • shift::UInt8: the right-shift

Returns

  • T: the mask defined by length and shift
CANalyze.Utils.maskMethod
mask(::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 mask
  • length::UInt8: the number of bits

Returns

  • T: the mask defined by length
CANalyze.Utils.to_bytesMethod
to_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 number num

Examples

using CANalyze.Utils
bytes = Utils.to_bytes(UInt16(0xAAFF))

# output
2-element Vector{UInt8}:
 0xff
 0xaa
CANalyze.Utils.zero_maskMethod
zero_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