我必须将十六进制字符串转换为十六进制整数,如下所示:
color = "0xFF00FF" #can be any color else, defined by functions
colorto = 0xFF00FF #copy of color, but from string to integer without changes
我也可以使用RGB格式。
我必须这样做,因为此函数会执行以下操作:
def i2s int, len
i = 1
out = "".force_encoding('binary')
max = 127**(len-1)
while i <= len
num = int/max
int -= num*max
out << (num + 1)
max /= 127
i += 1
end
out
end
我在这里看到存在十六进制整数。有人可以帮我解决这个问题吗?
您需要为String#to_i
方法提供整数基本参数:
irb> color = "0xFF00FF"
irb> color.to_i(16)
=> 16711935
irb> color.to_i(16).to_s(16)
=> "ff00ff"
irb> '%#X' % color.to_i(16)
=> "0XFF00FF"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句