我需要将一个十六进制字符串转换为十六进制整数,就像这样:
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
我在这里看到十六进制整数的存在。有人能帮我解决这个问题吗?
color.to_i
不是0
?你已经阅读了API文档(http://apidock.com/ruby/String/to_i),其中显示基数默认为“10”,因此带有零然后是‘x’的字符串将被视为十进制零。你需要告诉`to_i`方法,你的字符串是十六进制字符串,而API也有说明。 - Pavling