hexutils 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +82 -1
- data/Rakefile +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6395e84bd47bbfc360828297a903fe7a2ed2bd21b24cef24eae317b9dac1e7af
|
4
|
+
data.tar.gz: f7437d610cb255cb9d060abd9545ed52fd0942dd85a541e55764d5ae5b8f5afe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c3b815f9ed70e5d7da60a9ae3d8cc373feac6dcecd70195545969a09766fae81ad5335cb70c0111df20176e5247ca7e4412306074268bd53201cb770612661e
|
7
|
+
data.tar.gz: ae3c26841ac1acebef0afe9d5dd64158f2772687f520b9450c88fbd7f8d88f5354d3a991c0453c1161028d1f67cfc58515e774448263651f58d0d49df868c028
|
data/README.md
CHANGED
@@ -10,11 +10,92 @@ hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass, Ke
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
Part of the [**If I Were ~~King~~ Matz (aka Yukihiro Matsumoto) - Ideas For Ruby 4.0 - What's Broken & Missing in Ruby 3.x and How To Fix It**](https://github.com/rubycocos/core#if-i-were-king-matz-aka-yukihiro-matsumoto---ideas-for-ruby-40-----whats-broken--missing-in-ruby-3x-and-how-to-fix-it)
|
14
|
+
Series
|
15
|
+
|
13
16
|
|
14
17
|
|
15
18
|
## Usage
|
16
19
|
|
17
|
-
|
20
|
+
|
21
|
+
For some background on working with bin(ary) and hex(adecimal)
|
22
|
+
strings in ruby
|
23
|
+
see the [**Programming Bits, Bytes 'n' Blocks Step-by-Step Book / Guide**](https://github.com/rubycocos/core/tree/master/bytes#background----programming-bits-bytes-n-blocks-step-by-step-book--guide)
|
24
|
+
Let's start with the three types of strings, that is, bytes, string buffers, and frozen strings, ...
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
To make programming hex(adecimal) strings easier
|
29
|
+
let's add some encode / decode helpers.
|
30
|
+
|
31
|
+
|
32
|
+
### Kernel - `decode_hex` (or `hex`) and `encode_hex`
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
require 'hexutils'
|
36
|
+
|
37
|
+
# get a bin(ary) string from a hex(adecimal) string
|
38
|
+
hex '0xffff' # or
|
39
|
+
hex( '0xffff' ) # or
|
40
|
+
decode_hex( '0xffff' )
|
41
|
+
#=> "\xff\xff".b
|
42
|
+
|
43
|
+
# get a hex(a)decimal string from a bin(ary) string
|
44
|
+
encode_hex( "\xff\xff".b )
|
45
|
+
#=> "ffff"
|
46
|
+
```
|
47
|
+
|
48
|
+
Note: The decode_hex `decode_hex` (or `hex`)
|
49
|
+
allows all variants of upper- and lowercase
|
50
|
+
with or without the `0x`/`0X` prefix
|
51
|
+
e.g. '0XFFFF', 'FFFF', 'ffff', etc.
|
52
|
+
For convenience whitespace incl.
|
53
|
+
newlines are allowed anywhere.
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
### String - `to_hex` (or `hex`) and `hex?` (or `is_hex?`)
|
58
|
+
|
59
|
+
``` ruby
|
60
|
+
# get a hex(a)decimal string from a bin(ary) string
|
61
|
+
"\xff\xff".b.hex # or
|
62
|
+
"\xff\xff".b.to_hex # or
|
63
|
+
#=> "ffff"
|
64
|
+
|
65
|
+
"\xff\xff".b.hex? # or
|
66
|
+
"\xff\xff".b.is_hex?
|
67
|
+
#=> false
|
68
|
+
|
69
|
+
"ffff".hex? # or
|
70
|
+
"ffff".is_hex? # or
|
71
|
+
"0xffff".is_hex? # or
|
72
|
+
"0XFFFF".is_hex? # or
|
73
|
+
#=> true
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
NOTE: The "old" `String#hex` method that returns a integer number
|
78
|
+
from a hex(adecimal) string gets "monkey patched" or is that rewired?
|
79
|
+
to `String#old_hex` and `String#hexnum`.
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
### Bonus: NilClass - to_hex (or hex) and hex? (or is_hex?)
|
84
|
+
|
85
|
+
``` ruby
|
86
|
+
nil.hex? # or
|
87
|
+
nil.is_hex?
|
88
|
+
#=> false
|
89
|
+
|
90
|
+
nil.hex # or
|
91
|
+
nil.to_hex
|
92
|
+
#=> ''
|
93
|
+
```
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
That's it for now.
|
98
|
+
|
18
99
|
|
19
100
|
|
20
101
|
## License
|
data/Rakefile
CHANGED