diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 2a3c7d8892e23..33ec7e12ef5ad 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -26,16 +26,16 @@ maxintfloat() = maxintfloat(Float64) isinteger(x::AbstractFloat) = (trunc(x)==x)&isfinite(x) -num2hex(x::Float16) = hex(reinterpret(UInt16,x), 4) -num2hex(x::Float32) = hex(box(UInt32,unbox(Float32,x)),8) -num2hex(x::Float64) = hex(box(UInt64,unbox(Float64,x)),16) +hex(x::Float16) = hex(reinterpret(UInt16,x), 4) +hex(x::Float32) = hex(box(UInt32,unbox(Float32,x)),8) +hex(x::Float64) = hex(box(UInt64,unbox(Float64,x)),16) -function hex2num(s::AbstractString) - if length(s) <= 8 - return box(Float32,unbox(UInt32,parse(UInt32,s,16))) - end - return box(Float64,unbox(UInt64,parse(UInt64,s,16))) -end +num2hex{T<:Union{Float16, Float32, Float64}}(x::T) = hex(x) + +hex2num(s::AbstractString) = + (length(s) <= 8 + ? box(Float32,unbox(UInt32,parse(UInt32,s,16))) + : box(Float64,unbox(UInt64,parse(UInt64,s,16)))) @vectorize_1arg Number abs @vectorize_1arg Number abs2 diff --git a/base/intfuncs.jl b/base/intfuncs.jl index ad7ce992d801d..163fba8d829a3 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -267,7 +267,7 @@ function hex(x::Unsigned, pad::Int, neg::Bool) x >>= 4 i -= 1 end - if neg; a[1]='-'; end + neg && (a[1] = '-') ASCIIString(a) end diff --git a/base/strings/util.jl b/base/strings/util.jl index db6868a29209f..b6a5f88be8d0f 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -234,4 +234,19 @@ function hex2bytes(s::AbstractString) return a end -bytes2hex(arr::Vector{UInt8}) = join([hex(i,2) for i in arr]) +function hex(arr::Vector{UInt8}) + out = Vector{UInt8}(sizeof(arr)<<1) + o = 0 + for i = 1:sizeof(arr) + v = arr[i] + d = v>>4 + out[o += 1] = ('0' + d + 39 * (d > 9)) + d = v & 0xf + out[o += 1] = ('0' + d + 39 * (d > 9)) + end + ASCIIString(out) +end + +hex(str::ByteString) = hex(str.data) + +bytes2hex(arr::Vector{UInt8}) = hex(arr)