56 lines
1.3 KiB
Julia
56 lines
1.3 KiB
Julia
module bytes
|
|
|
|
export byte_vec_to_int, byte_vec_to_string, bit_at, bit_vector
|
|
|
|
"""
|
|
read_data_size(size::Vector{UInt8})::Real
|
|
|
|
FIT headers have a data size field, with 4 bytes.
|
|
"""
|
|
function byte_vec_to_int(byte_vec::Vector{UInt8})::Integer
|
|
result::Integer = 0
|
|
byte_vec = Int64.(byte_vec)
|
|
|
|
for i ∈ 0:length(byte_vec) - 1
|
|
result += byte_vec[1 + i] << (8 * i)
|
|
end
|
|
return result
|
|
end
|
|
|
|
"""
|
|
byte_vec_to_string(byte_vec::Vector{UInt8})::AbstractString
|
|
|
|
Given the byte vec, create a String
|
|
"""
|
|
function byte_vec_to_string(byte_vec::Vector{UInt8})::AbstractString
|
|
String(Char.(byte_vec))
|
|
end
|
|
|
|
"""
|
|
bit_at(byte::UInt8, idx::Int64; t::Type=Int64)::Int64
|
|
|
|
Parse the bit at specific index in byte
|
|
"""
|
|
function bit_at(byte::UInt8, idx::Int64; t::Type=Int64)::Int64
|
|
parse(t, bitstring(byte)[idx])
|
|
end
|
|
|
|
end
|
|
# byte_vec_to_string(UInt8.(collect("ABC")))
|
|
|
|
function bit_vector(byte::UInt8; little_endian=false)::Vector{UInt8}
|
|
range = if little_endian
|
|
8:-1:1
|
|
else
|
|
1:1:8
|
|
end
|
|
|
|
s = bitstring(byte)
|
|
[parse(UInt8, s[b]) for b in range]
|
|
end
|
|
|
|
|
|
function bit_vector(byte_vector::Vector{UInt8}; little_endian=false)::Vector{UInt8}
|
|
vecs = [bit_vector(byte; little_endian=little_endian) for byte ∈ byte_vector]
|
|
reduce(vcat, vecs)
|
|
end |