How To Make my Binary Translator Process Multiple Binary Numbers
I'm currently building a binary translator (binary to text) in the AppleScript editor. The code I wrote only lets you input one binary number at a time. I want to enter in multiple binary numbers at a time and still have the code translate it. I've been thinking of lots of different ways to have the code translate multiple numbers (binary), but everything I try doesn't seem to work. I'm relatively new to AppleScript, and I'm pretty stumped. Here's the code:
set binaryString to text returned of (display dialog "Binary Goes Here:" default answer "" with icon note buttons {"Cancel", "Translator"} default button 2 with title "Binary Translator") as string
set n8 to character 1 of binaryString
set n7 to character 2 of binaryString
set n6 to character 3 of binaryString
set n5 to character 4 of binaryString
set n4 to character 5 of binaryString
set n3 to character 6 of binaryString
set n2 to character 7 of binaryString
set n1 to character 8 of binaryString
if n8 = "0" then
set d8 to 0
else if n8 = "1" then
set d8 to 128
end if
if n7 = "0" then
set d7 to 0
else if n7 = "1" then
set d7 to 64
end if
if n6 = "0" then
set d6 to 0
else if n6 = "1" then
set d6 to 32
end if
if n5 = "0" then
set d5 to 0
else if n5 = "1" then
set d5 to 16
end if
if n4 = "0" then
set d4 to 0
else if n4 = "1" then
set d4 to 8
end if
if n3 = "0" then
set d3 to 0
else if n3 = "1" then
set d3 to 4
end if
if n2 = "0" then
set d2 to 0
else if n2 = "1" then
set d2 to 2
end if
if n1 = "0" then
set d1 to 0
else if n1 = "1" then
set d1 to 1
end if
set decimalString to (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8)
set asciiString to (ASCII character (decimalString))
return asciiString
Your script can be heavily simplified. What it looks like you're doing is converting a binary number into base 10 (denary), and then from there into a character code. (Side-note: It's always helpful to try and be explicit in describing what your code does--or is intending to do.)
set bits to every character in the text returned of ¬
(display dialog "Binary Input:" with icon note ¬
buttons {"Cancel", "Convert"} default button 2 ¬
with title "Binary to Text Conversion")
set |2ⁿ| to 1 -- powers of 2, i.e. 2⁰, 2¹, 2², 2³,..., 2⁷
set d to 0 -- the base 10 value (replaces your decimalString)
repeat with bit in the reverse of the bits
set d to d + bit * |2ⁿ|
set |2ⁿ| to 2 * |2ⁿ|
end repeat
set char to character id d
return char
You wish to repeat this process multiple times to convert as many binary numbers as the user supplies. There aren't a great many options for easily allowing multiple values to be obtained from a user. But it looks like you're experimenting with binary/text encoding of data, and that these binary numbers likely represent 8 bits of a single byte of data, therefore it seems reasonable to let a user supply as many bytes of data he wishes in a single input dialog, simply separating each binary value by a space, e.g.
"1011010 11010011 01011000 ..."
which would represent three bytes of data in binary form that are easy to split into individual items and converted one-by-one.
To this end, it makes sense to put the chunk of code that does the base conversion inside a handler
(function) that can be re-used again and again to perform as many conversions as one needs without having to write out identical code again and again.
Here's a draft of a script that does all of this:
set bytes to every word in the text returned of ¬
(display dialog "Binary Input:" with icon note ¬
buttons {"Cancel", "Convert"} default button 2 ¬
with title "Binary to Text Conversion")
set chars to {} -- this will fill with each decimal conversion
repeat with bits in bytes
-- call the handler below
set end of chars to the decimal from bits
end repeat
-- Now convert the decimal numbers to text
return character id chars
# A handler that takes a binary number and returns its decimal
# representation. The binary value can be passed in any of the
# following ways:
# 1. a list of characters of numbers
# e.g. {0, 1, 0, 1, 1, 0, 1, 0}, or
# {"0", "1", "0", "1", "1", "0", "1", "0"}
# 2. a string, e.g. "01011010"
# 3. a number, e.g. 1011010
to decimal from binary as {list, text, integer}
local binary
set |2ⁿ| to 1
set d to 0
tell a reference to binary to if its class ≠ list then ¬
set the contents to the characters of (it as text)
repeat with bit in the reverse of the binary
set d to d + bit * |2ⁿ|
set |2ⁿ| to 2 * |2ⁿ|
end repeat
return d
end decimal