5 Replies Latest reply on Jun 14, 2006 2:04 AM by Newsgroup_User

    convert decimal to hexa in director mx

    farhanakhan Level 1
      hi
      i wana convert decimal numbers in the hexa

      which type of function is use in director

      thx
        • 1. Re: convert decimal to hexa in director mx
          James Newton, ACP Level 3

          For integers from 0 to 255:

          on hex(aDecimal)
          return hexString(rgb(aDecimal, 0, 0)).char[2..3]
          end


          For larger integers and negative numberl

          on hex(aDecimal) -----------------------------------------------------
          -- INPUT: <aDecimal> must be an integer... unless you uncomment
          -- the error-checking code
          -- OUTPUT: Returns the hexadecimal string corresponding to any 4-
          -- byte integer, including negative numbers... or a self-
          -- explanatory error symbol if the error-checking code is
          -- uncommented and aDecimal is not an integer
          -- NOTE: Performance with negative numbers is about 7 times slower
          -- than with positive numbers
          --------------------------------------------------------------------

          -- -- Error-checking code
          -- if not integerP(aDecimal) then
          -- return #invalidInteger
          -- end if
          -- -- End of error-checking code

          if aDecimal > 0 then
          -- Use recursion if the number is positive
          hexDigit = aDecimal mod 16
          if hexDigit < 10 then
          tTemp = aDecimal / 16
          if tTemp then
          return hex(tTemp) & hexDigit
          else
          return string(hexDigit)
          end if
          else
          return hex(aDecimal / 16) & ("ABCDEF").char[hexDigit - 9]
          end if

          else if aDecimal then -- The number is negative
          -- Convert the number's positive counterpart, ...
          inverse = hex(aDecimal + the maxInteger + 1)
          -- ... pad with zeros if necessary, ...
          i = 8 - inverse.char.count
          if i then
          repeat while i
          put "0" before inverse
          i = i - 1
          end repeat
          -- ... then convert back to a negative number
          put 8 into char 1 of inverse
          else
          char1 = ("9ABCDEF").char[value(inverse.char[1])]
          put char1 into char 1 of inverse
          end if

          return inverse

          else -- aDecimal is 0
          return "0"
          end if
          end hex
          • 2. Re: convert decimal to hexa in director mx
            Level 7
            > For integers from 0 to 255:
            >
            > on hex(aDecimal)
            > return hexString(rgb(aDecimal, 0, 0)).char[2..3]
            > end

            That's positively devious! Talk about your unorthodox use of functions.
            Seems to me that you might be able to take this method futher by using the
            green and blue values as well, making it possible to encode numbers up to
            16,777,215. (256^3-1) Certainly it could be done this way using less code
            than your second example. Would have to do a side-by-side comparison to see
            which calculates faster, though.


            • 3. Re: convert decimal to hexa in director mx
              Level 7
              Darrel Hoffman wrote:
              >> For integers from 0 to 255:
              >>
              >> on hex(aDecimal)
              >> return hexString(rgb(aDecimal, 0, 0)).char[2..3]
              >> end
              >
              > That's positively devious! Talk about your unorthodox use of
              > functions. Seems to me that you might be able to take this method
              > futher by using the green and blue values as well, making it possible
              > to encode numbers up to 16,777,215. (256^3-1) Certainly it could be
              > done this way using less code than your second example. Would have
              > to do a side-by-side comparison to see which calculates faster,
              > though.

              We had all that fun years ago...
              http://groups.google.com/group/macromedia.director.lingo/browse_frm/thread/dcfe8d81db02011 b/c04482fb04068309?lnk=st&q=robert+tweed+rgb&rnum=5&hl=en#c04482fb04068309

              If you're only concerned with a limited range of numbers, then a lookup
              table is, of course, the ultimate for speed, demolishing the hexString()
              method by a factor of about 7. But who cares about microseconds :-)


              global hexLookup

              on hex(aDecimal)
              return hexString(rgb(aDecimal, 0, 0)).char[2..3]
              end

              on test
              -- set up lookup table
              hexLookup=[]
              repeat with i=0 to 255
              hexLookup[i+1]=hex(i)
              end repeat

              -- set things before loop
              now = the milliseconds
              the randomseed=1234567
              s=""
              repeat with i=1 to 500000
              s=hex(random(0,255))
              end repeat
              tim=the milliseconds-now
              put tim

              -- set things back the same way
              now = the milliseconds
              the randomseed=1234567
              s=""
              repeat with i=1 to 500000
              s=hexLookup[random(0,255)+1]
              end repeat
              tim=the milliseconds-now
              put tim
              end test

              -- Welcome to Director --
              test()
              -- 7162
              -- 983

              Andrew

              • 4. Re: convert decimal to hexa in director mx
                Level 7
                > If you're only concerned with a limited range of numbers, then a lookup
                > table is, of course, the ultimate for speed, demolishing the hexString()
                > method by a factor of about 7. But who cares about microseconds :-)

                Well, yeah, but a lookup table is cheating, since you're not really doing
                any calculations at all. Plus keeping a table of any significant size in
                memory would have a bit of a ram impact. A lookup table capable of dealing
                with numbers in the 16,000,000 range would probably be a bit hefty. I'd
                like to compare the hexString() method, modified to factor in green and blue
                values, compared to the second method in openspark's post. They both share
                the ability to deal with much larger numbers. (Though the hexString()
                method lacks negatives.) I'm actually surprised that Director doesn't have
                a function like this built in. Every other language I've used (other than
                Integer Basic on the old Apple II) has had the ability to convert to hex
                with a single command, as well as octal and binary. It's the numbers the
                computer uses, it should be able to calculate with them much faster than it
                handles decimals. Also missing are cool functions like bitshifts, which are
                MUCH faster than standard arithmetic functions if you know how to use them.

                Sometimes I wish human beings had only 4 fingers on each hand instead of 5.
                Then we wouldn't be stuck with this awkward numbering system we use today.
                To heck with getting the US and UK to switch to full metric like everyone
                else. Let's do it right and get the whole world to switch to octal or hex
                instead. Boy, that would screw up the math departments at every school on
                the planet...


                • 5. Re: convert decimal to hexa in director mx
                  Level 7
                  Darrel Hoffman wrote:
                  >> If you're only concerned with a limited range of numbers, then a
                  >> lookup table is, of course, the ultimate for speed, demolishing the
                  >> hexString() method by a factor of about 7. But who cares about
                  >> microseconds :-)
                  >
                  > Well, yeah, but a lookup table is cheating, since you're not really
                  > doing any calculations at all.

                  You have to do them once, up front. Just imagine if you had to do
                  /everything/ from first principles, every time.

                  [Wandering a bit OT here, but my computer's motherboard died in the
                  furnace we're working in and the replacement doesn't arrive until
                  tomorrow.]

                  If I have seen farther than others, it is because I was standing on the
                  shoulders of giants.
                  - Isaac Newton

                  In the sciences, we are now uniquely privileged to sit side by side with
                  the giants on whose shoulders we stand.
                  - Gerald Holton

                  If I have not seen as far as others, it is because giants were standing
                  on my shoulders.
                  - Hal Abelson

                  In Computer Science, we stand on each other's feet.
                  - Brian Reid

                  If I have pinged farther than others, it is because I routed upon the
                  T3s of giants.
                  - Unknown

                  [back to the topic]

                  > Plus keeping a table of any
                  > significant size in memory would have a bit of a ram impact. A
                  > lookup table capable of dealing with numbers in the 16,000,000 range
                  > would probably be a bit hefty.

                  I refer you to the first line of my message you quoted... but yes,
                  having a lookup table of some 16,000,000 elements would rather defeat
                  the object if it ended up being paged out to disk.

                  > I'd like to compare the hexString()
                  > method, modified to factor in green and blue values, compared to the
                  > second method in openspark's post.

                  If you follow the link I posted, you'll find Robert Tweed has already
                  coded that.

                  > They both share the ability to
                  > deal with much larger numbers. (Though the hexString() method lacks
                  > negatives.)

                  Robert's code allows for negative numbers.

                  > Also missing are
                  > cool functions like bitshifts, which are MUCH faster than standard
                  > arithmetic functions if you know how to use them.

                  Aren't they available in ECMAscript in Director?

                  > Sometimes I wish human beings had only 4 fingers on each hand instead
                  > of 5. Then we wouldn't be stuck with this awkward numbering system we
                  > use today. To heck with getting the US and UK to switch to full
                  > metric like everyone else.

                  There's also a lot to be said for duodecimal (the wikipedia entry rather
                  does it to death). And you can use your fingers for counting in binary
                  if you really want to, but the digit you use to represent one might get
                  very tired :-)

                  > Let's do it right and get the whole world
                  > to switch to octal or hex instead. Boy, that would screw up the math
                  > departments at every school on the planet...

                  But think of the childre^Wparents!

                  Andrew