Quantcast
Channel: Small Basic
Viewing all articles
Browse latest Browse all 669

Text to Binary Converter - Small Basic Featured Program

$
0
0

Ha! I saw an episode of "Big Bang Theory" where they were arguing over the binary translation of English (how many zeros and ones there should be).

Amir had made a Decimal to Binary converter that I featured, and I mentioned that a text to binary converter would be pretty sweet.

 

Well now (through the magic of Small Basic) Amir CPS has made it possible to have a conversation with zeros and ones that you have encoded in binary:

Try it out here!

   

Get your geek on! Go translate this into English! You can double click this one line, and then copy all the text at once:

010000110110111101101101011011010110010101101110011101000010000001101111011011100010000001110100011010000110010100100000011000100110110001101111011001110010000001111001011011110111010101110010001000000110011001100001011101100110111101110010011010010111010001100101001000000110001101101111011011000110111101110010

Or you can paste in the four lines, one at a time, no spaces:

01000011011011110110110101101101011001010110111001110100001000000110111101101110001

0000001110100011010000110010100100000011000100110110001101111011001110010000001111

0010110111101110101011100100010000001100110011000010111011001101111011100100110100

10111010001100101001000000110001101101111011011000110111101110010

 

It's a text to binary and binary to text converter!

Import : FTK803

Small change in codes to handle "NewLine" character

Import : BTN178

And here's the code...


1.GraphicsWindow.Width = 400
2.GraphicsWindow.Height = 300
3.GraphicsWindow.Title = "Text to Binary :
Binary to Text"
4.
5.TextBox =
Controls.AddMultiLineTextBox(10,10)
6.Controls.SetSize(TextBox,380,250)
7.ButtonTB =
Controls.AddButton("Convert to
Binary"
,10,265)
8.ButtonBT =
Controls.AddButton("Convert to
Text"
,140,265)
9.Controls.ButtonClicked =
onClick
10.
11.Sub onClick
12.  LastButton =
Controls.LastClickedButton
13.  If LastButton =
"Button1" Then
14.    ConvertToBinary()
15.  Else
16.    ConvertToText()
17.  EndIf
18.EndSub
19.Sub ConvertToBinary
20.  String = Controls.GetTextBoxText(TextBox)
21.  For i = 1 To Text.GetLength(String)
22.    CharCode =
Text.GetCharacterCode(Text.GetSubText(String,i,1))
23.    'convert ascii codes into
binary
24.    bit = ""
25.    binval = ""
26.    Count = 0
27.    While CharCode >
0
28.      bit[Count] = Math.Remainder(CharCode,2)
29.      CharCode =
Math.Floor(CharCode
/ 2)
30.      Count = Count + 1
31.    EndWhile
32.    For j = Array.GetItemCount(bit) To 0 Step -1
33.      binval = Text.Append(binval,bit[j])
34.    EndFor
35.    'add leading zero to make binary
value even
36.    For b = 0 To 8 - Text.GetLength(binval)
37.      binval = Text.Append(0,binval)
38.    EndFor
39.    longbin =
Text.Append(longbin,binval)
40.  EndFor
41.  Controls.SetTextBoxText(TextBox,longbin)
42.  longbin =
""
43.EndSub
44.Sub ConvertToText
45.  Binary = Controls.GetTextBoxText(TextBox)
46.  If Math.Remainder(Text.GetLength(Binary),8) <> 0 Then
47.    GraphicsWindow.ShowMessage("Binary is un
even"
,"Error")
48.  Else
49.    For g = 1 To Text.GetLength(Binary) Step 8
50.      Binarychar =
Text.GetSubText(Binary,g,8)
51.      'Convert binary to
decimal
52.      For bit_Count =
1 To Text.GetLength(Binarychar)
53.        binaryNum =
binaryNum + Text.GetSubText(Binarychar,Text.GetLength(Binarychar)-bit_Count+1,1)*Math.Power(2,bit_Count-1)
54.      EndFor
55.      'get char from ascci
code
56.      Char = Text.GetCharacter(binaryNum)
57.      binaryNum =
""
58.      'append char
59.      LongString =
Text.Append(LongString,Char)
60.    EndFor
61.    Controls.SetTextBoxText(TextBox,LongString)
62.    LongString =
""
63.  EndIf
64.EndSub

 

 

What do you think?

Do you need a Klingon to Binary converter? =^)

Enjoy!

   - Ninja Ed


Viewing all articles
Browse latest Browse all 669

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>