如何在Brightscright中使用整数值创建十六进制字符串?

时间:2015-07-28 02:22:27

标签: hex decimal roku brightscript

我编写了一个Int 2 Hex函数,并想知道这是否是最好的方法

documentation

有没有其他方法可以做到这一点?

' @param intValue positive integers from 0 onwards
function IntHexString(intValue as Integer) as String
 this = {}
 this.hexString = function(int) as String
    num = int/16.0
    remainder = (num mod 1) * 16.0
    result = fix(num)

    if remainder > 15
        hex = m.hexString(remainder)
    else
        hex = m.determineHex(remainder)
    end if

    if (result > 0) hex = m.hexString(result) + hex

    retun hex
 end function

 ' 0 --> 15
 this.determineHex = function(int) as String
    if int = 15 then return "F"
    if int = 14 then return "E"
    if int = 13 then return "D"
    if int = 12 then return "C"
    if int = 11 then return "B"
    if int = 11 then return "A"
    return int
 end function

 return this.hexString(intValue)
end function

1 个答案:

答案 0 :(得分:1)

是的,还有更好的方法 - 使用stri(val, radix),如下所示:

BrightScript Debugger> ? &h12abcdef
 313249263
BrightScript Debugger> ? stri(313249263, 16)
12abcdef