hex_values 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p194
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hex_values.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sergio Rafael Gianazza
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # HexValues
2
+
3
+ Transform your float to hexadecimals and viceversa!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hex_values'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hex_values
18
+
19
+ ## Usage
20
+
21
+ require the gem:
22
+
23
+ require 'hex_values'
24
+
25
+ And then transform any float to a hexadecimal:
26
+
27
+ irb> 234.25.to_hex
28
+ => "EA.4"
29
+
30
+ Or transform any hexadecimal to the corresponding float:
31
+
32
+ irb> "EA.4".to_float
33
+ => 234.25
34
+
35
+
36
+ ## Contributing
37
+
38
+ ### Forking the project
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
45
+
46
+ ### Creating an issue
47
+
48
+ Go to the issue tab and create a new issue explaining what is happening and what it the expected behaviour.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ t.libs.push 'spec'
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hex_values/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Sergio Rafael Gianazza"]
6
+ gem.email = ["sgianazza@gmail.com"]
7
+ gem.description = %q{Add functionality to get hexadecimal values from float and float from a string hexadecimal}
8
+ gem.summary = %q{Convert any float or fixnum to the correct representation in hexadecimal, and convert any hexadecimal string in their corresponding float representation.}
9
+ gem.homepage = "http://github.com/gianu/hex_values"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "hex_values"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HexValues::VERSION
17
+
18
+ gem.add_development_dependency "minitest"
19
+ gem.add_development_dependency "minitest-reporters"
20
+ end
@@ -0,0 +1,3 @@
1
+ module HexValues
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hex_values.rb ADDED
@@ -0,0 +1,69 @@
1
+ require "hex_values/version"
2
+
3
+ module HexValuesFromFloat
4
+ MAX_DECIMALS = 14
5
+
6
+ def to_hex
7
+ num,dec = get_hex_value(self)
8
+ first = num
9
+ second = ""
10
+ iterations = 0
11
+ while dec > 0 && iterations < MAX_DECIMALS
12
+ num, dec = get_hex_value(dec*16)
13
+ second << num
14
+ iterations += 1
15
+ end
16
+
17
+ if "".eql? second
18
+ first
19
+ else
20
+ "#{first}.#{second}"
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def get_hex_value(float_num)
27
+ base, remainder = float_num.to_s.split('.')
28
+ [base.to_i.to_s(16).upcase, "0.#{remainder}".to_f]
29
+ end
30
+ end
31
+
32
+ module FloatValuesFromString
33
+ def to_float
34
+ number = 0
35
+ base, remainder = self.split('.')
36
+
37
+ if base
38
+ arr=[]
39
+ base.each_char { |c| arr << c }
40
+ arr.reverse!.each_index do |index|
41
+ number += arr[index].to_i(16) * (16 ** index)
42
+ end
43
+ end
44
+
45
+ if remainder
46
+ arr=[]
47
+ remainder.each_char { |c| arr << c }
48
+ arr.each_index do |index|
49
+ number += arr[index].to_i(16).to_f / (16 ** (index + 1))
50
+ end
51
+ end
52
+
53
+ number
54
+ end
55
+ end
56
+
57
+ class Float
58
+ include HexValuesFromFloat
59
+ end
60
+
61
+ class String
62
+ include FloatValuesFromString
63
+ end
64
+
65
+ class Fixnum
66
+ def to_hex
67
+ self.to_f.to_hex
68
+ end
69
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'HexValues' do
4
+ describe "float to hex" do
5
+ it "converts 234.25 to EA.4" do
6
+ 234.25.to_hex.must_equal "EA.4"
7
+ end
8
+
9
+ it "converts 1875.37 to 753.5EB851EB85" do
10
+ 1875.37.to_hex.must_equal "753.5EB851EB851EB8"
11
+ end
12
+
13
+ it "converts 5476.28 to 1564.47AE147AE1" do
14
+ 5476.28.to_hex.must_equal "1564.47AE147AE147AE"
15
+ end
16
+
17
+ it "converts 7763783.89 to 767747.E3D70A3C" do
18
+ 7763783.89.to_hex.must_equal "767747.E3D70A3D70A3D7"
19
+ end
20
+
21
+ it "converts 1 to 1" do
22
+ 1.to_hex.must_equal "1"
23
+ end
24
+
25
+ it "converts 10 to A" do
26
+ 10.to_hex.must_equal "A"
27
+ end
28
+
29
+ it "converts 0.25 to 0.4" do
30
+ 0.25.to_hex.must_equal "0.4"
31
+ end
32
+
33
+ it "converts 0.21 to 0.35C28F5C28F5C2" do
34
+ 0.21.to_hex.must_equal "0.35C28F5C28F5C2"
35
+ end
36
+
37
+ it "converts 0 to 0" do
38
+ 0.to_hex.must_equal "0"
39
+ end
40
+ end
41
+
42
+ describe "hex to float" do
43
+ it "converts EA.4 to 234.25" do
44
+ "EA.4".to_float.must_equal 234.25
45
+ end
46
+
47
+ it "converts 753.5EB851EB85 to 1875.37" do
48
+ "753.5EB851EB85".to_float.must_equal 1875.37
49
+ end
50
+
51
+ it "converts 1564.47AE147AE1 to 5476.28" do
52
+ "1564.47AE147AE1".to_float.must_equal 5476.28
53
+ end
54
+
55
+ it "converts 767747.E3D70A3C to 7763783.89" do
56
+ "767747.E3D70A3C".to_float.must_equal 7763783.89
57
+ end
58
+
59
+ it "converts 1 to 1" do
60
+ "1".to_float.must_equal 1.0
61
+ end
62
+
63
+ it "converts A to 10" do
64
+ "A".to_float.must_equal 10.0
65
+ end
66
+
67
+ it "converts 0.4 to 0.25" do
68
+ "0.4".to_float.must_equal 0.25
69
+ end
70
+
71
+ it "converts 0.35C28F5C28F5C2 to 0.21" do
72
+ "0.35C28F5C28F5C2".to_float.must_equal 0.21
73
+ end
74
+
75
+ it "converts an empty string to 0" do
76
+ "".to_float.must_equal 0
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'minitest/reporters'
4
+
5
+ require 'hex_values'
6
+
7
+ Minitest::Reporters.use!
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hex_values
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sergio Rafael Gianazza
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest-reporters
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Add functionality to get hexadecimal values from float and float from
47
+ a string hexadecimal
48
+ email:
49
+ - sgianazza@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .ruby-version
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - hex_values.gemspec
61
+ - lib/hex_values.rb
62
+ - lib/hex_values/version.rb
63
+ - spec/hex_values_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: http://github.com/gianu/hex_values
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Convert any float or fixnum to the correct representation in hexadecimal,
89
+ and convert any hexadecimal string in their corresponding float representation.
90
+ test_files:
91
+ - spec/hex_values_spec.rb
92
+ - spec/spec_helper.rb