-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
hex2bytes for Vector{UInt8} #23267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hex2bytes for Vector{UInt8} #23267
Changes from 11 commits
82e337d
1d1c0f2
2165807
3db1d84
3312ac0
cae96cd
b8a48af
7d6dd61
ce6abc9
1fc8eb4
c60e85d
4c2f17a
d6f2582
5c9fbd5
6f5fe83
8cd51b5
71c7d60
f09607a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -762,6 +762,7 @@ export | |
| graphemes, | ||
| hex, | ||
| hex2bytes, | ||
| hex2bytes!, | ||
| ind2chr, | ||
| info, | ||
| is_assigned_char, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -463,12 +463,90 @@ function hex2bytes(s::AbstractString) | |
| return a | ||
| end | ||
|
|
||
| """ | ||
| hex2bytes(s::AbstractVector{UInt8}) | ||
|
|
||
| Convert the hexadecimal bytes array to its binary representation. Returns | ||
| `Vector{UInt8}`, i.e. a vector of bytes. | ||
|
||
| """ | ||
|
||
| @inline function hex2bytes(s::AbstractVector{UInt8}) | ||
| len = length(s) | ||
| if isodd(len) | ||
| throw(ArgumentError("source vector length must be even")) | ||
| end | ||
| d = zeros(UInt8, div(len, 2)) | ||
|
||
| return hex2bytes!(d, s) | ||
| end | ||
|
|
||
| """ | ||
| hex2bytes!(d::AbstractVector{UInt8}, s::AbstractVector{UInt8}) | ||
|
|
||
| Converts the hexadecimal bytes vector to its binary representation. The results are | ||
|
||
| populated into a destination vector. The function returns the destination vector. | ||
|
|
||
| # Examples | ||
| ```jldoctest | ||
| julia> s = UInt8["01abEF"...] | ||
|
||
| 6-element Array{UInt8,1}: | ||
| 0x30 | ||
| 0x31 | ||
| 0x61 | ||
| 0x62 | ||
| 0x45 | ||
| 0x46 | ||
|
|
||
| julia> d =zeros(UInt8, 3) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in last check-in. |
||
| 3-element Array{UInt8,1}: | ||
| 0x00 | ||
| 0x00 | ||
| 0x00 | ||
|
|
||
| julia> hex2bytes!(d, s) | ||
|
||
| 3-element Array{UInt8,1}: | ||
| 0x01 | ||
| 0xab | ||
| 0xef | ||
| ``` | ||
| """ | ||
| function hex2bytes!(d::AbstractVector{UInt8}, s::AbstractVector{UInt8}) | ||
| i, j = start(s), 0 | ||
| # This line is important as this ensures computation happens in word boundary and not | ||
| # byte boundary. Boundary computation can be almost 10 times slower | ||
| n::UInt = 0 | ||
| c1::UInt = 0 | ||
| c2::UInt = 0 | ||
|
||
| while !done(s, i) | ||
| n = 0 | ||
|
||
| c1, i = next(s, i) | ||
| done(s, i) && throw(ArgumentError("source vector length must be even")) | ||
|
||
| c2, i = next(s, i) | ||
| n = number_from_hex(c1) | ||
| n <<= 4 | ||
| n += number_from_hex(c2) | ||
| d[j+=1] = (n & 0xFF) | ||
|
||
| end | ||
| return d | ||
| end | ||
|
|
||
| @inline function number_from_hex(c::UInt) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it seems like this function should return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-word boundary computation can be 10 times slower or that's what was observed. |
||
| DIGIT_ZERO = UInt('0') | ||
| DIGIT_NINE = UInt('9') | ||
| LATIN_UPPER_A = UInt('A') | ||
| LATIN_UPPER_F = UInt('F') | ||
| LATIN_A = UInt('a') | ||
| LATIN_F = UInt('f') | ||
|
||
|
|
||
| return (DIGIT_ZERO <= c <= DIGIT_NINE) ? c - DIGIT_ZERO : | ||
|
||
| (LATIN_UPPER_A <= c <= LATIN_UPPER_F) ? c - LATIN_UPPER_A + 10 : | ||
| (LATIN_A <= c <= LATIN_F) ? c - LATIN_A + 10 : | ||
| throw(ArgumentError("not a hexadecimal number: '$(Char(c))'")) | ||
| end | ||
|
|
||
| """ | ||
| bytes2hex(bin_arr::Array{UInt8, 1}) -> String | ||
|
|
||
| Convert an array of bytes to its hexadecimal representation. | ||
| All characters are in lower-case. | ||
|
|
||
| # Examples | ||
| ```jldoctest | ||
| julia> a = hex(12345) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will need to be added to the stdlib doc index somewhere to show up in the rendered docs https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#adding-a-new-docstring-to-base
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.