Jack Bauer wrote:
> I need it to be more like this:
>
> "FFFFFF".whatever_magic_method # => 0xFFFFFF
I think at this point, the issue is that you need to deal with how to
display the integer, not conver ti.
"FF".to_i(16) produces the INTEGER: 255 which is the same as 0xFF.
So, if you want to display the INTEGER 255 as hex, use either printf
"0x%0x" or to_s(16). For instance:
Ruby code:
s = "0xFF"
puts "Convert string [#{s}] to an integer:"
n = s.to_i(16)
puts "normal puts: #{n}"
printf "printf in hex: 0x%x\n", n
puts "using to_s(16): #{n.to_s(16)}"
Outputs:
Convert string [0xFF] to an integer:
normal puts: 255
printf in hex: 0xff
using to_s(16): ff
Hope that helps.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---