Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
formatting — Gideros Forum

formatting

MoKaLuxMoKaLux Member
edited September 2020 in General questions
Can you please help me? I have this:
local mycolor = 0x0000ff

how can I put it in a string without it being converted to 255? I would like to get 0x0000ff or at least 0000ff. I tried:

print(tostring(mycolor)) -- 255
print(mycolor) -- 255
print(string.format("%q", mycolor)) -- "255"

Thank you.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • rrraptorrrraptor Member
    edited September 2020 Accepted Answer
    print( ("0x%06x"):format(math.random(0xffffff)) )
    or
    print( ("%x"):format(math.random(0xffffff)) )

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • thank you :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • keszeghkeszegh Member
    Accepted Answer
    this kind of notation is scifi for me, so perhaps you might also prefer:
    string.format("%x",0xffffff)
    which is the same but written differently.

    Likes: MoKaLux, antix

    +1 -1 (+2 / -0 )Share on Facebook
  • true, your example is also perfect :) thank you keszegh.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited September 2020 Accepted Answer
    keszegh said:

    this kind of notation is scifi for me, so perhaps you might also prefer:
    string.format("%x",0xffffff)
    which is the same but written differently.

    :D
    It basically means this:
    local myString = "0x%06x" -- means print "0x" as regular string, then add converted hex number with 6 leading zeros
    -- so if you call "print(("%x"):format(0x0000ff))" then you will get just "ff", but with "%06x" result is "0000ff"
    print(myString:format(0x0000ff))
    -- or
    print(string.format(myString, 0x0000ff))

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Accepted Answer
    And if you want to write a string as its hex representation:
    local s="Hello"
    print(("%02X"):rep(#s):format(s:byte(1,#s)))

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • you read my mind, I wanted to ask the question :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.