@@ -504,38 +504,21 @@ function hex(x::Unsigned, pad::Int, neg::Bool)
504
504
end
505
505
506
506
"""
507
- num2hex(f )
507
+ reinterpret(T::Type, str::AbstractString, b=16 )
508
508
509
- A hexadecimal string of the binary representation of a number.
510
- See also the [`bits`](@ref) function, which is similar but gives
511
- a binary string, and [`hex2num`](@ref) which does the opposite conversion.
509
+ Interprets a string as the bit representation of a
510
+ number of type `T`, encoded in base `b`.
512
511
513
512
```jldoctest
514
- julia> num2hex(Int64(4))
515
- "0000000000000004"
516
-
517
- julia> num2hex(2.2)
518
- "400199999999999a"
519
- ```
520
- """
521
- num2hex (n:: Union{Bool,BitReal} ) = hex (reinterpret (Unsigned, n), sizeof (n)* 2 )
522
-
523
- """
524
- hex2num(T::Type, str)
525
-
526
- Interprets a hexadecimal string as the bit representation of a
527
- number of type `T`. See also [`num2hex`](@ref).
528
-
529
- ```jldoctest
530
- julia> hex2num(Float64, "400199999999999a")
513
+ julia> reinterpret(Float64, "400199999999999a")
531
514
2.2
532
515
533
- julia> hex2num (Int32, "fffffffe")
516
+ julia> reinterpret (Int32, "fffffffe")
534
517
-2
535
518
```
536
519
"""
537
- hex2num (:: Type{T} , s:: AbstractString ) where {T<: Union{Bool,BitReal} } =
538
- reinterpret (T, parse (reinterpret (Unsigned, T), s, 16 ))
520
+ reinterpret (:: Type{T} , s:: AbstractString , b = 16 ) where {T<: Union{Bool,Char ,BitReal} } =
521
+ reinterpret (T, parse (reinterpret (Unsigned, T), s, b ))
539
522
540
523
const base36digits = [' 0' :' 9' ;' a' :' z' ]
541
524
const base62digits = [' 0' :' 9' ;' A' :' Z' ;' a' :' z' ]
@@ -626,25 +609,29 @@ Convert an integer to a decimal string, optionally specifying a number of digits
626
609
dec
627
610
628
611
"""
629
- bits(n)
612
+ bits(n, fmt=bin )
630
613
631
614
A string giving the literal bit representation of a number.
632
- See also the [`num2hex`](@ref) function, which is similar but
633
- gives a hexadecimal string.
615
+ The `fmt` function, which can be `bin` or `hex`,
616
+ determines the format used to represent those bits
617
+ respectively as a binary or hexadecimal string.
634
618
635
619
```jldoctest
636
620
julia> bits(4)
637
621
"0000000000000000000000000000000000000000000000000000000000000100"
638
622
623
+ julia> bits(4, hex)
624
+ "0000000000000004"
625
+
639
626
julia> bits(2.2)
640
627
"0100000000000001100110011001100110011001100110011001100110011010"
628
+
629
+ julia> bits(2.2, hex)
630
+ "400199999999999a"
641
631
```
642
632
"""
643
- bits (x:: Union{Bool,Int8,UInt8} ) = bin (reinterpret (UInt8,x),8 )
644
- bits (x:: Union{Int16,UInt16,Float16} ) = bin (reinterpret (UInt16,x),16 )
645
- bits (x:: Union{Char,Int32,UInt32,Float32} ) = bin (reinterpret (UInt32,x),32 )
646
- bits (x:: Union{Int64,UInt64,Float64} ) = bin (reinterpret (UInt64,x),64 )
647
- bits (x:: Union{Int128,UInt128} ) = bin (reinterpret (UInt128,x),128 )
633
+ bits (n:: Union{Bool,Char,BitReal} , fmt:: Union{typeof(bin), typeof(hex)} = bin) =
634
+ fmt (reinterpret (Unsigned, n), sizeof (n)* (fmt === bin ? 8 : 2 ))
648
635
649
636
"""
650
637
digits([T<:Integer], n::Integer, base::T=10, pad::Integer=1)
0 commit comments