This output looked really weird at first, but it's actually consistent with our base-ten floats. 1.012
looks normal to us just because we use the decimal numeral system every day.
If I understand it correctly, the output can be seen as a rational with the numerator being the whole hex number (without '.') and the denominator being 16**(length of "decimal" part)
:
def string_to_float(string, base = 10)
string.delete('.').to_i(base).to_f / base**(string.reverse.index('.') || 0)
end
string_to_float('1.03126e978d4fe', 16)
#=> 1.012
string_to_float('1c.8', 16)
#=> 28.5
string_to_float('3.243f6a8885a3', 16)
#=> 3.141592653589793
string_to_float('20.0', 16)
#=> 32.0
string_to_float('20.', 16)
#=> 32.0
string_to_float('20', 16)
#=> 32.0
string_to_float('3.14159', 10)
#=> 3.14159
string_to_float('11.001001000011111101101010100010001000010110100011', 2)
#=> 3.141592653589793
If you only need to convert hex floats, you can remove the base
argument :
def hex_string_to_float(hex)
hex.delete('.').to_i(16).to_f / 16**(hex.reverse.index('.') || 0)
end
I am trying to make an image looks like it is kind of transparent? I don't really know how to describe it but it is like you can write texts on it and the image is in the back. is this doable with ...
I am trying to make an image looks like it is kind of transparent? I don't really know how to describe it but it is like you can write texts on it and the image is in the back. is this doable with ...
How can add a currency prefix to a TextInput in React Native? Here is an example of what I'm trying to achieve: To give a bit more details - the TextInput should have a default value, for instance £...
How can add a currency prefix to a TextInput in React Native? Here is an example of what I'm trying to achieve: To give a bit more details - the TextInput should have a default value, for instance £...
I'm working on an electron app(first time using electron). I'm also very new to javascript so please excuse my stupidity. In the index.js file I have this method to check if a network connection ...
I'm working on an electron app(first time using electron). I'm also very new to javascript so please excuse my stupidity. In the index.js file I have this method to check if a network connection ...
I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(...
I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher: beforeEach(...