Created
October 22, 2016 07:08
-
-
Save vaiorabbit/e25f728fdd443c6462d5d0dcaff1d0cc to your computer and use it in GitHub Desktop.
Shows the floating point representation of a hexadecimal string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# ex.) $ ruby h2f.rb 0000803f -> 1.0 | |
# $ ruby h2f.rb c3f54840 -> 3.140000 | |
# $ ruby h2f.rb 182d4454fb210940 -> 3.141592653589793 | |
# $ ruby h2f.rb 400921fb54442d18 -b -> 3.141592653589793 | |
require 'getoptlong' | |
$cmd_opts = GetoptLong.new( | |
[ '-b', GetoptLong::NO_ARGUMENT ], | |
) | |
$opt_big = false | |
$cmd_opts.each do |opt, arg| | |
case opt | |
when '-b'; $opt_big = true | |
end | |
end | |
if $0 == __FILE__ | |
values = ARGV.select {|arg| /\H/.match(arg) == nil } # == !arg[/\H/] | |
values.each do |value| | |
hex_str = value | |
if $opt_big | |
re = hex_str.scan(/(\h\h)/) | |
hex_str = "" | |
re.reverse.each do |byte| | |
hex_str += byte[0] | |
end | |
end | |
print "#{value}\t" | |
case value.length | |
when 8 | |
printf "%.6f\n", [hex_str].pack('H8').unpack('F')[0] | |
when 16 | |
puts [hex_str].pack('H16').unpack('D') | |
else | |
print "Unknown\n" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment