7 Replies Latest reply on Mar 31, 2008 6:50 PM by Newsgroup_User

# Hex?

Is there a method for reading Hex values (and other non-decimal base
numbers) in Lingo? I mean, I could probably hash together some rough-shod
method going character by character and saying "A=10", "B=11", etc., but
it'd be much better if there was some natively-supported method of doing
this where I could just say hexToDec(someHexVal) or decToHex(someDecVal) and
have it done automatically. Likewise I imagine a Binary converter could be
useful, possibly even Alphanumeric hashes, or Octal (does anyone really use
Octal?). Of course I imagine if there isn't something like this in there
natively, one of you guys has probably built one by now...

• ###### 1. Re: Hex?
If you're using MX2004 then create a javascript moviescript and put this:

function decTohex(d) {return d.toString(16)}

function hexTodec(h) {return parseInt(h,16)}
• ###### 2. Re: Hex?
> If you're using MX2004 then create a javascript moviescript and put this:
>
> function decTohex(d) {return d.toString(16)}
>
> function hexTodec(h) {return parseInt(h,16)}

Hmm. Thanks. Although one of these days I ought to learn Javascript so I
can understand why that works, because that code makes no sense to me. Is
the "16" in here just the desired base? Like, could I set it to 36 for an
alphanumeric hash, 2 for binary, 8 for octal, etc.? (And theoretically
could it be extended to use weird things like base 15, 17, etc.? Not that I
want to, just curious how this thing actually works...)

• ###### 3. Re: Hex?
Never mind, answered my own question. Nifty. Funny that it can only be
done in javascript though. I thought that pretty much everything could be
done in either language, depending on which you were most comfortable with.
This is the first time I've found something that Lingo won't do for some
reason...

• ###### 4. Re: Hex?
I just thought it's much easier doing it in javascript than in Lingo... so if you have MX2004 then 2 lines is a no-brainer... here's the same code in Lingo:

-- snagged (mod'ed a bit) from mediamacros
on hexTodec hexNum
hexdigits = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
numstring = 0
cnt = hexNum.length
repeat with x = 1 to cnt
numString = numString * 16
numString = numString + hexDigits.getOne(hexNum.char[x]) - 1
end repeat
return numString
end

on decTohex input
hexdigits = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
done = false
output = ""
repeat while not done
remainder = (input mod 16) + 1
put hexdigits[remainder] before output
if input < 16 then
exit repeat
else
input = input / 16
end if
end repeat
return output
end
• ###### 5. Re: Hex?
>I just thought it's much easier doing it in javascript than in Lingo... so
>if
> you have MX2004 then 2 lines is a no-brainer... here's the same code in
> Lingo:

<snipped code>
Ouch, that IS a lot more code.

What I find odd is that the Director compiler (as of MX2004 at least)
obviously has access to nifty functions like those one-liner Javascript
things, but it's only able to use them in Javascript mode, and can't somehow
transfer that capability over to the Lingo side. (The functions even appear
in the drop-down lists while in Lingo mode, but it doesn't seem possible to
use them.) I mean, doesn't it all get compiled into machine code in the end
anyhow? Seems to me that it shouldn't matter what language you code
something in, the end result should be essentially the same (especially if
they're being compiled by the same program). Makes me wonder how many other
hidden features there are that are only accessible in Javascript mode, or if
there's any Lingo-only functions as well...

• ###### 6. Re: Hex?
Code is "translated" into something called P-Code when
compiled/published, but it still requires a run-time interpreter.
Director is, after all, a scripting IDE, not compiled to machine code.
Which interpreter executes the compiled code depends on whether it's
JavaScript code (the SpiderMonkey engine runs it) or Lingo code (the
"usual" lingo engine {IML?} runs it)
• ###### 7. Hex?
what should be allowed is a simple tag or somesuch that can be used to indicate when you're writing javascript so you can write it in the same script member instead of the member(x).scriptSyntax being global.. or the scriptSyntax can still be global and the tag just allows you to have it in the same member for organizational purposes... when it's compiled who cares at that point if the compiler places it separately or what it does with it... for me it would make things much more organized - I don't care if it's Javascript or Lingo; I care if it should be part of the same parent script and that the Javascript version is 1 line of code instead of 14... take, for instance, Javascript's Math, Date and Regular Expression Objects... they're so much more robust than anything Lingo has.

-- lingo code here
on someHandler
..
....
end

[start javascript]

// start javascript here
function() {
..
....
}

[end javascript]

-- continue lingo here
on someOtherHandler
..
....
end