Convert string to hexadecimal (preserving meaning)


#1

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?


#2

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


#3

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


#4

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


#5

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


#6

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.


#7

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 :slight_smile:

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.