Search code examples
hexdecimalrokubrightscript

how can i make a hex string from integer value in Brightscright?


I have written an Int 2 Hex function and was wondering if this is the best way

https://gist.github.com/kaayr1m/cacd3432b53fd854b0de

are there any other ways to do this?

' @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

Solution

  • Yes, there is a better way - use stri(val, radix), like so:

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