I''m using a charting library that takes color values in hex, so white would be 0xffffff. This is a number, not a string, so doing something like "0x" + my_color_variable wouldn''t work. If I have a variable from user input that is "FFFFFF", how can I convert it to the hex number 0xffffff? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-16 16:09 UTC
Re: Convert string to hexadecimal (preserving meaning)
On Apr 16, 4:47 pm, Jack Bauer <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m using a charting library that takes color values in hex, so white > would be 0xffffff. This is a number, not a string, so doing something > like "0x" + my_color_variable wouldn''t work. > > If I have a variable from user input that is "FFFFFF", how can I convert > it to the hex number 0xffffff?the to_i method on string takes an optional argument (the base to use). Fred> -- > Posted viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> > the to_i method on string takes an optional argument (the base to > use). > > FredYes, but it doesn''t convert it the way I need it to. What I mean is: "FFFFFF".to_i(base=16) # => 16777215 I need it to be more like this: "FFFFFF".whatever_magic_method # => 0xFFFFFF -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Apr-16 16:42 UTC
Re: Convert string to hexadecimal (preserving meaning)
Jack Bauer wrote: [...]> > Yes, but it doesn''t convert it the way I need it to. What I mean is: > > "FFFFFF".to_i(base=16) # => 16777215 > > I need it to be more like this: > > "FFFFFF".whatever_magic_method # => 0xFFFFFFSo wrap it: class String def parse_hex self.to_i 16 end end What''s the big deal? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-16 17:04 UTC
Re: Convert string to hexadecimal (preserving meaning)
On Apr 16, 5:20 pm, Jack Bauer <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote:> > Yes, but it doesn''t convert it the way I need it to. What I mean is: > > "FFFFFF".to_i(base=16) # => 16777215 > > I need it to be more like this: > > "FFFFFF".whatever_magic_method # => 0xFFFFFFThose are exactly the same integer - the console just shows them to you in base 10 by default. to_s takes a similar base argument if you need to get a string out of it again. Fred> -- > Posted viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jack Bauer wrote:> I need it to be more like this: > > "FFFFFF".whatever_magic_method # => 0xFFFFFFI 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well , i just wanted to say a big THANKS!!! to Joshua for the solution, and Jack for posting up this problem which was the same as i was facing.. This post came up when i googled for it. And the solution mentioned above solved my problem.. Thanks a ton!! to both of you :) Joshua Ball wrote in post #806177:> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.