Forum: Ruby on Rails Convert string to hexadecimal (preserving meaning)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jack B. (Guest)
on 2009-04-16 19:47
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?
Frederick C. (Guest)
on 2009-04-16 20:18
(Received via mailing list)
On Apr 16, 4:47 pm, Jack B. <removed_email_address@domain.invalid>
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
Jack B. (Guest)
on 2009-04-16 20:20
Frederick C. wrote:
>
> the to_i method on string takes an optional argument (the base to
> use).
>
> Fred

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 # => 0xFFFFFF
Marnen L. (Guest)
on 2009-04-16 20:42
Jack B. 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 # => 0xFFFFFF

So 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
removed_email_address@domain.invalid
Frederick C. (Guest)
on 2009-04-16 21:06
(Received via mailing list)
On Apr 16, 5:20 pm, Jack B. <removed_email_address@domain.invalid>
wrote:
> Frederick C. 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 # => 0xFFFFFF

Those 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
Joshua B. (Guest)
on 2009-04-16 21:12
Jack B. 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.
Anna T. (Guest)
on 2012-01-16 14:57
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 B. wrote in post #806177:
> Jack B. 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.
This topic is locked and can not be replied to.