将十六进制字符串转换为十六进制int [英] Convert a hex string to a hex int
本文介绍了将十六进制字符串转换为十六进制int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将十六进制字符串转换为十六进制整数,如下所示:
I have to convert a hexadecimal string to a hexadecimal integer, like this:
color = "0xFF00FF" #can be any color else, defined by functions
colorto = 0xFF00FF #copy of color, but from string to integer without changes
我也可以使用RGB格式.
I can have RGB format too.
我有义务这样做,因为该函数在后面执行:
I'm obliged to do this because this function goes after :
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
我在此处看到存在十六进制整数.有人可以帮我解决这个问题吗?
I saw here that hexadecimal integers exist. Can someone help me with this problem?
推荐答案
您需要为 String#to_i
方法提供整数基本参数:
You'd need supply integer base argument to String#to_i
method:
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"
这篇关于将十六进制字符串转换为十六进制int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文