Home > Mobile >  Ruby: Converting Hexadecimal Strings to Hexadecimal NON-string (edited)
Ruby: Converting Hexadecimal Strings to Hexadecimal NON-string (edited)

Time:11-16

Lately, I've been struggling to search online for ways to convert hexadecimal strings into hexadecimal actuals. As an example, "0xffffffff" -> 0xffffffff. After loading the JSON file (which cannot store hexadecimal directly), the stored integer value, 4294967295, was successfully converted back to "0xffffffff" by using the following example code:

hex_str = "0x" << 4294967295.to_s(16) #--> "0xffffffff"

The real frustration is that I cannot seem to find a Ruby way to recreate that hexadecimal value without being a String datatype... I really hope I'm not overlooking anything. My reason for the use of non-string hexadecimals is to utilize them for Gosu-coloring notation. I do not want to use Gosu's Color class (inputting rgb values [255, 255, 255]) as it slows the performance drastically when many rectangular quad_draw() objects are generated in-game (it went down to about 42 fps from 60 fps when 600 rects were drawn). The program did run at 60 fps when I hard coded in the hexadecimal actuals (not of string datatypes), so I'm confident that using these values in that format are the way to go. This is something I'm looking for:

hex_int = hex_str.some_function_to_hex #--> 0xffffffff

Any ideas would be greatly appreciated. If you could share a way that could convert 4294967295 to 0xffffffff directly, that would be a bonus! Thanks in advance to all here! :)

CodePudding user response:

You can directly pass integer to Gosu::Color.new to create color

3.0.0 :002 > Gosu::Color.new(4294967295)
 => #<Gosu::Color:ARGB=0xff_ffffff>

Or Gosu::Color.argb

3.0.0 :003 > Gosu::Color.argb(4294967295)
 => #<Gosu::Color:ARGB=0xff_ffffff>
  • Related