Microsoft Small Basic

Program Listing: CRP718
GraphicsWindow.Width = 450
GraphicsWindow.Height = 300
GraphicsWindow.Title = "Text to Binary : Binary to Text"

TextBox = Controls.AddMultiLineTextBox(10,10)
Controls.SetSize(TextBox,430,250)
ButtonTB = Controls.AddButton("Convert to Binary",10,265)
ButtonBT = Controls.AddButton("Convert to Text",140,265)
ButtonTH = Controls.AddButton("To Hex",260,265)
ButtonHT = Controls.AddButton("From Hex",325,265)
Controls.ButtonClicked = onClick

Sub onClick
LastButton = Controls.LastClickedButton
If LastButton = "Button1" Then
ConvertToBinary()
ElseIf LastButton = "Button2" Then
ConvertToText()
Elseif LastButton = "Button3" Then
ToHex()
Else
ConvertBack()
EndIf
EndSub
Sub ConvertToBinary
String = Controls.GetTextBoxText(TextBox)
For i = 1 To Text.GetLength(String)
CharCode = Text.GetCharacterCode(Text.GetSubText(String,i,1))
temp = CharCode
'convert ascii codes into binary
bit = ""
binval = ""
Count = 0
While CharCode > 0
bit[Count] = Math.Remainder(CharCode,2)
CharCode = Math.Floor(CharCode / 2)
Count = Count + 1
EndWhile
For j = Array.GetItemCount(bit) To 0 Step -1
binval = Text.Append(binval,bit[j])
EndFor
'add leading zero to make binary value even
if temp = 13 Or temp = 10 Then
lead = 10
Else
lead = 8
EndIf
For b = 0 To lead - Text.GetLength(binval)
binval = Text.Append(0,binval)
EndFor
longbin = Text.Append(longbin,binval)
EndFor
Controls.SetTextBoxText(TextBox,longbin)
longbin = ""
EndSub
Sub ConvertToText
Binary = Controls.GetTextBoxText(TextBox)
Flag = 0
'check if input is a valid binary
For k = 1 To 255
If k < 48 Or k > 49 Then
If Text.IsSubText(Binary,Text.GetCharacter(k)) Then
Flag = Flag + 1
EndIf
EndIf
EndFor
'make sure that binary is even
If Math.Remainder(Text.GetLength(Binary),8) <> 0 Or Flag > 0 Then
If Flag > 0 Then
GraphicsWindow.ShowMessage("Either the text you entered is already in text form or you have enterd a wrong binary representation of the text."+Text.GetCharacter(10)+"Try Converting it to Binary.?","Error")
Else
GraphicsWindow.ShowMessage("Binary is uneven","Error")
EndIf
Else
For g = 1 To Text.GetLength(Binary) Step 8
Binarychar = Text.GetSubText(Binary,g,8)
'Convert binary to decimal
For bit_Count = 1 To Text.GetLength(Binarychar)
binaryNum = binaryNum + Text.GetSubText(Binarychar,Text.GetLength(Binarychar)-bit_Count+1,1)*Math.Power(2,bit_Count-1)
EndFor
'get char from ascci code
Char = Text.GetCharacter(binaryNum)
binaryNum = ""
'append char
LongString = Text.Append(LongString,Char)
EndFor
Controls.SetTextBoxText(TextBox,LongString)
LongString = ""
EndIf
EndSub
Sub ToHex
TempText = Controls.GetTextBoxText(TextBox)
TLen = Text.GetLength(TempText)
For i = 1 To TLen
CharCode = Text.GetCharacterCode(Text.GetSubText(TempText,i,1))
quotient = CharCode
countX = 0
Hstring = ""
While quotient <> 0
temp = Math.Remainder(quotient,16)
If temp < 10 Then
temp = temp + 48
Else
temp = temp + 55
EndIf
hex[countX] = Text.GetCharacter(temp)
quotient = Math.Floor(quotient/16)
countX = countX + 1
EndWhile
For y = Array.GetItemCount(hex) To 0 Step -1
Hstring = Text.Append(Hstring,hex[y])
EndFor
If Text.GetLength(Hstring) <> 2 Then
Hstring = Text.Append(0,Hstring)
EndIf
bighex = Text.Append(bighex,Hstring)
EndFor
Controls.SetTextBoxText(TextBox,bighex)
bighex = "
EndSub

Sub ConvertBack
TempText = Controls.GetTextBoxText(TextBox)
TLen = Text.GetLength(TempText)
If Math.Remainder(TLen,2) = 0 Then
For i = 1 To TLen Step 2
Char = Text.GetSubText(TempText,i,2)
'Convert Hex to Dec | code by Nonki Takahashi
Dec = 0
HLen = Text.GetLength(Char)
For Ptr = 1 To HLen
Dec = Dec * 16 + Text.GetIndexOf("0123456789ABCDEF", Text.GetSubText(Char, Ptr, 1)) - 1
EndFor
Letter = Text.GetCharacter(Dec)
bigchar = Text.Append(bigchar,Letter)
EndFor
Controls.SetTextBoxText(TextBox,bigchar)
bigchar = ""
Else
GraphicsWindow.ShowMessage("Invalid Hexadecimal string","Error")
EndIf
EndSub