@@ -475,24 +475,29 @@ julia> hex2bytes(a)
475
475
"""
476
476
function hex2bytes end
477
477
478
- hex2bytes (s:: AbstractString ) = hex2bytes (Vector {UInt8} (String (s)))
479
- hex2bytes (s:: AbstractVector{UInt8} ) = hex2bytes! (Vector {UInt8} (uninitialized, length (s) >> 1 ), s)
478
+ hex2bytes (s:: AbstractString ) = hex2bytes (String (s))
479
+ hex2bytes (s:: Union{String,AbstractVector{UInt8}} ) = hex2bytes! (Vector {UInt8} (uninitialized, length (s) >> 1 ), s)
480
+
481
+ _firstbyteidx (s:: String ) = 1
482
+ _firstbyteidx (s:: AbstractVector{UInt8} ) = first (eachindex (s))
483
+ _lastbyteidx (s:: String ) = sizeof (s)
484
+ _lastbyteidx (s:: AbstractVector{UInt8} ) = endof (s)
480
485
481
486
"""
482
- hex2bytes!(d::AbstractVector{UInt8}, s::AbstractVector{UInt8})
487
+ hex2bytes!(d::AbstractVector{UInt8}, s::Union{String, AbstractVector{UInt8} })
483
488
484
489
Convert an array `s` of bytes representing a hexadecimal string to its binary
485
490
representation, similar to [`hex2bytes`](@ref) except that the output is written in-place
486
491
in `d`. The length of `s` must be exactly twice the length of `d`.
487
492
"""
488
- function hex2bytes! (d:: AbstractVector{UInt8} , s:: AbstractVector{UInt8} )
489
- if 2 length (d) != length (s)
490
- isodd (length (s)) && throw (ArgumentError (" input hex array must have even length" ))
493
+ function hex2bytes! (d:: AbstractVector{UInt8} , s:: Union{String, AbstractVector{UInt8} } )
494
+ if 2 length (d) != sizeof (s)
495
+ isodd (sizeof (s)) && throw (ArgumentError (" input hex array must have even length" ))
491
496
throw (ArgumentError (" output array must be half length of input array" ))
492
497
end
493
498
j = first (eachindex (d)) - 1
494
- for i = first ( eachindex (s)) : 2 : endof (s)
495
- @inbounds d[j += 1 ] = number_from_hex (s[i]) << 4 + number_from_hex (s[ i+ 1 ] )
499
+ for i = _firstbyteidx (s) : 2 : _lastbyteidx (s)
500
+ @inbounds d[j += 1 ] = number_from_hex (_nthbyte (s,i)) << 4 + number_from_hex (_nthbyte (s, i+ 1 ) )
496
501
end
497
502
return d
498
503
end
0 commit comments