Re: Converting Hex to binary
Re: Converting Hex to binary
- Subject: Re: Converting Hex to binary
- From: deivy petrescu <email@hidden>
- Date: Sat, 8 Jan 2005 20:34:28 -0500
On Jan 8, 2005, at 2:40 PM, Bernardo Hoehl wrote:
Hi List!
I know that I over work my scripts all the time.
But this is common to newbyes. Ins't it?
I have this script that aparently is functional:
set theInput to "0309"
set HexList to every character of ("0123456789ABCDEF")
set binList to {"0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
"1110", "1111"}
set ConvertedBin to ""
repeat with i from 1 to the count of theInput
repeat with ii from 1 to the count of HexList
if item i of theInput is item ii of HexList then
set ConvertedBin to ConvertedBin & item ii of binList
end if
end repeat
end repeat
return ConvertedBin --returns "0000001100001001"
Please:
Could you comment my script?
I bet you would write it in a shorter way.
thanks!
Bernardo Höhl
Rio de Janeiro - Brazil _______________________________________________
Bernardo, I'll be uploading a new version of my math libraries soon,
but while the new version is not there, you can use this script, which
is fairly fast, to convert numbers between any to integer bases from 2
to 16:
<script>
on iBaseA2B(x, a, b) -- changes the integer x in base 2 ≤ a ≤ 16 to
base 2 ≤ b ≤ 16.
if 1 * a = 1 * b or x is in {0, 1} then return x
try
if ((a div 1) < a) or (a < 2) or (a > 16) or ((b div 1) < b) or (b <
2) or (b > 16) then error
if (character 1 of (x as string)) = "-" then
set {x, signal} to {text 2 through -1 of (x as string), "-"}
else
set {x, signal} to {(x as string), ""}
end if
if a * 1 ≠ 10 then
set cchar to length of x
set z to 0
ignoring case
repeat with i from 1 to cchar
set co to (offset of (character i of x) in mystring) - 1
if co < a then
set z to z + co * (a ^ (cchar - i))
else
error
end if
end repeat
end ignoring
set x to z
end if
if 1 * b = 10 then
return signal & x
else
set newx to {x mod b}
set x to (x - (x mod b))
repeat
set x to x div b
set end of newx to (x mod b)
if x < a then exit repeat
end repeat
set numx to {}
repeat with l in (reverse of newx)
set end of numx to (character (l + 1) of mystring)
end repeat
set beginning of numx to signal
return numx as string
end if
on error
error "Either " & x & " is not in the base " & a & " or " & a & "
or " & b & " are not valid bases. "
end try
end iBaseA2B
</script>
Note, the script does not handle numbers in the scientific notation
because of the ambiguity. Is 1E8 a decimal or a number in base 15 or
16?
The routine above computes conversion of integer only, the routines
however can convert rational any number in any of the above base to
another.
deivy
--------------------------------------------
Agora quem da bola é o Santos,
Salve o novo campeão!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden