baseConvert() & 32-bit ops

Dar Scott dsc at swcp.com
Sat Jun 21 19:59:00 EDT 2003


On Saturday, June 21, 2003, at 05:05 PM, Mark Brownell wrote:

> I don't see what to place into the local variable halfBlock
>
> -- my goal is to end up with this: 1181708909
> -- from this:
> put charToNum("m") + (charToNum("r") * 256 ) + ( charToNum("o") * 
> 65536 ) + ( charToNum("F") * 16777216 )

Hey, you changed order on me!  Actually, that is the order I prefer.

The number is placed in halfBlock.  The function returns the number of 
values converted.

Try this in a button (I'm assuming fields "Input" and "Report"):

-- ====================
on mouseUp
   put strToHalfBlock(field "Input") into field "Report"
end mouseUp

function strToHalfBlock str
   local halfBlock
   put binaryDecode("N",str,halfBlock) into numConverted
   if numConverted is not 1 then
     -- do error thing here?
     return "error"
   end if
   return halfBlock
end strToHalfBlock

function charsToHalfBlock a, b, c, d
   return strToHalfBlock(a&b&c&d)
end charsToHalfBlock
-- =====================

If you prefer individual chars, you can use the later function.

This script puts 1181708909 into the field "Report" when the field 
"Input" contains "Form".  Hey!  That's just what you asked for!

Try the TD, too, for info.

The "N" is for network byte order unsigned 32-bit.  That way, once you 
get the order the way you want, it will be the same across platforms.  
The other option is host byte order.  Should we ask for options for 
big-endian and little-endian, too?

You can loop through your padded string to be encrypted four chars at a 
time to create a halfBlock array (or process as you go).  If 'i' is 
your looping counter, then pick off four chars at a time with this 
expression 'char i to i+3 of paddedString' or something like that.  
Repeat has been upgraded to allow increments by 4 (or 8 if you process 
as you go).

Dar Scott









More information about the use-livecode mailing list