kenbrueck
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2012
05:41 PM
Convert Int to Hex?
Is there a simple way to convert an integer to hex in Brightscript?
For example, given 255 it would return FF (or some similar form, e.g. 0xFF, 0x0000FF, etc.)
For example, given 255 it would return FF (or some similar form, e.g. 0xFF, 0x0000FF, etc.)
5 REPLIES 5
MSGreg
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2012
06:12 PM
Re: Convert Int to Hex?
Easiest way, I think, is to go through a byte array.
The following is based on the excellent libRokuDev project, from the file rdByteArray.brs
I've modified it to run faster, test before using
The following is based on the excellent libRokuDev project, from the file rdByteArray.brs
I've modified it to run faster, test before using
' * Convert an integer to a hex string *
function ConvertINTtoHEX(num as integer) as string
return rdINTtoBA(num).toHexString()
end function
' * Convert an integer to a (4 byte) roByteArray *
function fastINTtoBA(num as integer) as object
ba = CreateObject("roByteArray")
ba.setresize(4, false)
i% = num
ba[3] = i%
i% = i% / 256 : ba[2] = i% ' using % auto converts float from division to int
i% = i% / 256 : ba[1] = i% ' then assigning to bytes auto truncates (AND &h0ff)
i% = i% / 256 : ba[0] = i%
return ba
end function
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2012
06:32 PM
Re: Convert Int to Hex?
Here's another way:
function decToHex (dec as integer) as string
hexTab = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
hex = ""
while dec > 0
hex = hexTab [dec mod 16] + hex
dec = dec / 16
end while
if hex = "" return "0" else return hex
end function
GPF
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2012
08:56 PM
Re: Convert Int to Hex?
"MSGreg" wrote:
Easiest way, I think, is to go through a byte array.
Me too
BrightScript Debugger> ba = CreateObject("roByteArray"):ba.push(255):?ba.toHexString()
FF
Troy
kenbrueck
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2012
01:56 PM
Re: Convert Int to Hex?
Great! Thanks for the answers AND for introducing me to libRokuDev!
kartigas
Newbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2021
02:28 PM
Re: Convert Int to Hex?
Function addOpacityToHexColor(color as String, opacity as float) as String opacityHex = StrI(Cint(opacity * 255), 16) ' Converting Float / Integer to Hex return "#" + color + opacityHex End Function