Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
sshika

Decimal to hex

Question

Hello

 

I realizes a program to convert decimal in hexa.

local function hexa(dec)
local B,K,OUT,I,D = 16,"0123456789ABCDEF","",0
  if dec == 0 then
    OUT = "00"
  elseif dec > 0 and dec < 16 then
    D = dec + 1
    OUT = "0"..string.sub(K,D,D)
  elseif dec > 16 and dec < 256 then
    V1 = math.modf(dec/B)+1
    V2 = (dec-V1*16)
    OUT = string.sub(K,V1,V1)..string.sub(K,V2,V2)
  end
  return OUT
end

this program convert a lower number in 256, and i use this in a formule

local function rod(x,y,n)
  lvl = br.getControlRodLevel(n)
  rouge = math.floor((100-lvl)*255/100)
  vert = math.floor(lvl*255/100)
  color = "#"..hexa(rouge)..hexa(vert).."00"
  setColor(color,0x0000FF)
  gpu.set(x,y,"█"..string.format("%3s",lvl).."█")
  setColor(0x0, 0xFFFFFF)
end

if i print color, i obtain a value "#00FF00" to change color, but the format is not valid. There is a function to change format of this value ?

 

thank you.

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

By the way, if you want to display a number as hexadecimal, it's much easier to use string.format:

("%x"):format(256) -- = "100"

But as said above, color values aren't strings, meaning that you don't need to convert them to any specfic number format. 0x100 and 256 are both of the type number and are equal.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.