hexutils 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/Manifest.txt +6 -0
- data/README.md +32 -0
- data/Rakefile +30 -0
- data/lib/hex.rb +3 -0
- data/lib/hexutils.rb +95 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 58d93d37042b54496a85566264d827f2852ed034ecf6356b63ec121561230347
|
4
|
+
data.tar.gz: 999e485f063f2e1753f9541e94bd34deb2a0c609bb26b03e551be0b234ffe7c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04aeca2de8a683f4d3a5080e23cb10ceb3d38aa580bbaa127b267315e59fe38bd4da3de416cdf6081d233395d35772091b6389e1fe5f1d2750c84849e80145dc
|
7
|
+
data.tar.gz: 5f978fc664a4398e8148c96812875910a6db8629e1448acec559efe76c368efa79f8d4c30c3accb55fb480069a270cab00eb6eac6ac2ad34111a685f94590583
|
data/CHANGELOG.md
ADDED
data/Manifest.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Hex(adecimal) Encode / Decode Helpers - From Hex(adecimal) String to Bin(ary) String And Back
|
2
|
+
|
3
|
+
hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass, Kernel and more
|
4
|
+
|
5
|
+
|
6
|
+
* home :: [github.com/rubycocos/core](https://github.com/rubycocos/core)
|
7
|
+
* bugs :: [github.com/rubycocos/core/issues](https://github.com/rubycocos/core/issues)
|
8
|
+
* gem :: [rubygems.org/gems/hexutils](https://rubygems.org/gems/hexutils)
|
9
|
+
* rdoc :: [rubydoc.info/gems/hexutils](http://rubydoc.info/gems/hexutils)
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To be done
|
18
|
+
|
19
|
+
|
20
|
+
## License
|
21
|
+
|
22
|
+
The scripts are dedicated to the public domain.
|
23
|
+
Use it as you please with no restrictions whatsoever.
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
## Questions? Comments?
|
29
|
+
|
30
|
+
Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake).
|
31
|
+
Thanks!
|
32
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
|
3
|
+
|
4
|
+
Hoe.spec 'hexutils' do
|
5
|
+
|
6
|
+
self.version = '0.0.1'
|
7
|
+
|
8
|
+
self.summary = "hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass, Kernel and more"
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = { home: 'https://github.com/rubycocos/core' }
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'wwwmake@googlegroups.com'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.md'
|
18
|
+
self.history_file = 'CHANGELOG.md'
|
19
|
+
|
20
|
+
self.extra_deps = [
|
21
|
+
]
|
22
|
+
|
23
|
+
self.licenses = ['Public Domain']
|
24
|
+
|
25
|
+
self.spec_extras = {
|
26
|
+
required_ruby_version: '>= 2.3'
|
27
|
+
}
|
28
|
+
|
29
|
+
end
|
30
|
+
|
data/lib/hex.rb
ADDED
data/lib/hexutils.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
##
|
5
|
+
## todo/check:
|
6
|
+
## allow/ignore whitespaces [ \t\n\r] in hex strings why? why not?
|
7
|
+
##
|
8
|
+
## or add option with ignore e.g. ':' or such - why? why not?
|
9
|
+
|
10
|
+
class String
|
11
|
+
## use deprecated_hex or legacy_hex
|
12
|
+
## or ??? such? - why? why not?
|
13
|
+
alias_method :old_hex, :hex
|
14
|
+
alias_method :hexnum, :old_hex
|
15
|
+
|
16
|
+
def hex?
|
17
|
+
if self.empty?
|
18
|
+
## make empty string (e.g.'') a valid hex string - why? why not?
|
19
|
+
## note: 0x (prefix only) is NOT a valid hex string
|
20
|
+
true
|
21
|
+
elsif self =~ /\A(0[xX])?[0-9a-fA-F]+\z/
|
22
|
+
## note: allow 0x0 or 0xf too or 0xfff or 0x123
|
23
|
+
## that is uneven hexstrings
|
24
|
+
## a byte assumes always two chars
|
25
|
+
true
|
26
|
+
else
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias_method :is_hex?, :hex?
|
31
|
+
|
32
|
+
def to_hex
|
33
|
+
if self.empty?
|
34
|
+
## change encoding to utf_8 - why? why not?
|
35
|
+
## always return a new string why? why not?
|
36
|
+
'' ## note: assume utf_8 encoding for new string
|
37
|
+
else
|
38
|
+
## note: always return a hex string with default encoding e.g. utf-8 - why? why not?
|
39
|
+
### check if unpack always works on bytes (or .b required???)
|
40
|
+
self.unpack( 'H*' ).first.force_encoding( Encoding::UTF_8 )
|
41
|
+
end
|
42
|
+
end
|
43
|
+
alias_method :hex, :to_hex
|
44
|
+
end # class String
|
45
|
+
|
46
|
+
|
47
|
+
class NilClass
|
48
|
+
def hex?() false; end
|
49
|
+
alias_method :is_hex?, :hex?
|
50
|
+
|
51
|
+
##
|
52
|
+
# open question - add hex/to_hex to NilClass too (along like to_s/to_i/etc.)
|
53
|
+
# why? why not?
|
54
|
+
def to_hex
|
55
|
+
'' ## note: assume utf_8 encoding for new string
|
56
|
+
end
|
57
|
+
alias_method :hex, :to_hex
|
58
|
+
end # class NilClass
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
module Kernel
|
63
|
+
def encode_hex( bin ) ## bin_to_hex
|
64
|
+
## use ArgumentError or TypeError - why? why not?
|
65
|
+
raise TypeError, "Value must be a string" unless bin.is_a?( String )
|
66
|
+
## note: always return a hex string with default encoding e.g. utf-8 - why? why not?
|
67
|
+
bin.hex
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def decode_hex( hex )
|
72
|
+
## use ArgumentError or TypeError - why? why not?
|
73
|
+
raise TypeError, "Value must be a string" unless hex.is_a?( String )
|
74
|
+
## note: for now allow whitespaces - get auto-removed - why? why not?
|
75
|
+
hex = hex.gsub( /[ \t\r\n]/, '' )
|
76
|
+
if hex.empty? ## special case - empty string
|
77
|
+
''.b
|
78
|
+
else
|
79
|
+
raise TypeError, 'Non-hexadecimal char found' unless hex =~ /\A(0[xX])?[0-9a-fA-F]+\z/
|
80
|
+
|
81
|
+
## allow optional starting 0x - why? why not?
|
82
|
+
hex = hex[2..-1] if ['0x', '0X'].include?( hex[0,2] )
|
83
|
+
|
84
|
+
hex = '0'+hex if hex.size.odd?
|
85
|
+
[hex].pack('H*')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
alias_method :hex, :decode_hex
|
89
|
+
|
90
|
+
## open question - add "global" bin_to_hex and hex_to_bin alias to - why? why not?
|
91
|
+
|
92
|
+
# alias_method :bin_to_hex, :encode_hex ## add bin_to_hex alias - why? why not?
|
93
|
+
# alias_method :hex_to_bin, :decode_hex ## add hex_to_bin alias - why? why not?
|
94
|
+
end # module Kernel
|
95
|
+
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hexutils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gerald Bauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: hoe
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.23'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.23'
|
47
|
+
description: hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass,
|
48
|
+
Kernel and more
|
49
|
+
email: wwwmake@googlegroups.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files:
|
53
|
+
- CHANGELOG.md
|
54
|
+
- Manifest.txt
|
55
|
+
- README.md
|
56
|
+
files:
|
57
|
+
- CHANGELOG.md
|
58
|
+
- Manifest.txt
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/hex.rb
|
62
|
+
- lib/hexutils.rb
|
63
|
+
homepage: https://github.com/rubycocos/core
|
64
|
+
licenses:
|
65
|
+
- Public Domain
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- "--main"
|
70
|
+
- README.md
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.3'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: hexutils - hex(adecimal) encode/decode helpers 'n' more for String, NilClass,
|
88
|
+
Kernel and more
|
89
|
+
test_files: []
|