Microsoft Small Basic

Program Listing:
Embed this in your website
' Text to Binary and Binary to Text conversion (c) Amir CPS [ BTN178 ]
' ADDED: Binary to Base64 and Base64 to Binary WhTurner 2013-02-05

GraphicsWindow.Width = 500
GraphicsWindow.Height = 300
GraphicsWindow.Title = "Text to Binary : Binary to Text [ and Base64-conversion ]"

TextBox = Controls.AddMultiLineTextBox(10,10)
Controls.SetSize(TextBox,460,250)
ButtonTB = Controls.AddButton("Text to Binary",10,265)
ButtonBT = Controls.AddButton("Binary to Text",120,265)
ButtonB64=Controls.AddButton("Binary to Base64",225,265)
Button64B=Controls.AddButton("Base64 to Binary",350,265)
Controls.ButtonClicked = onClick

Sub onClick
  LastButton = Controls.LastClickedButton
  If LastButton = "Button1" Then
    ConvertToBinary()
  ElseIf LastButton="Button2" then
    ConvertToText()
  elseif LastButton="Button3" then
    ConvertToBase64()
  elseif LastButton="Button4" then
    ConvertFromBase64()
  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
  Longstring=""
  Binary = Controls.GetTextBoxText(TextBox)
  If Math.Remainder(Text.GetLength(Binary),8) <> 0 Then
    GraphicsWindow.ShowMessage("Binary is uneven","Error")
  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 ConvertToBase64
  Longstring=""
  Binary = Controls.GetTextBoxText(TextBox)
  If Math.Remainder(Text.GetLength(Binary),8) <> 0 Then
    GraphicsWindow.ShowMessage("Binary is uneven","Error")
  Else
    If Math.Remainder(Text.GetLength(Binary),6) <> 0 Then
      xx=6-Math.Remainder(Text.GetLength(Binary),6)
      Binary=text.Append(Binary,text.GetSubText("00000",1,xx))
    endif
    For g = 1 To Text.GetLength(Binary) Step 6
      Binarychar = Text.GetSubText(Binary,g,6)   ''111111
      'Convert binary to decimal '0 - 63
      For bit_Count = 1 To Text.GetLength(Binarychar)   ''6
        binaryNum = binaryNum * 2 + Text.GetSubText(Binarychar,bit_Count,1)
      EndFor
      If binaryNum<26 Then
        B64=text.GetCharacter(binaryNum+65)
      ElseIf binaryNum<52 then
        B64=text.GetCharacter(binaryNum+71)
      ElseIf binaryNum<62 then
        B64=text.GetCharacter(binaryNum-4)
      ElseIf binaryNum=62 then
        B64="+"
      ElseIf binaryNum=63 then
        B64="/"
      endif
      Longstring=text.Append(Longstring,B64)
      binaryNum=""
    EndFor
  endif
  Controls.SetTextBoxText(TextBox,LongString)
EndSub   'ConvertToBase64

Sub ConvertFromBase64
  Longbin=""
  String = Controls.GetTextBoxText(TextBox)
  For i = 1 To Text.GetLength(String)
    CharCode = Text.GetCharacterCode(Text.GetSubText(String,i,1))
    If CharCode=43 Then
      temp=62
    elseif CharCode=47 Then
      temp=63
    elseif CharCode<58 Then
      temp=CharCode+4
    elseif CharCode<91 Then
      temp=CharCode-65
    elseif CharCode<123 Then
      temp=CharCode-71
    EndIf
    'convert ascii codes into binary
    bit = ""
    binval = ""
    Count = 0
    While temp > 0
      bit[Count] = Math.Remainder(temp,2)
      temp = Math.Floor(temp / 2)
      Count = Count + 1
    EndWhile
    For j = Array.GetItemCount(bit) To 0 Step -1
      binval = Text.Append(binval,bit[j])
    EndFor
    xx=6-text.GetLength(binval)
    binval=text.Append(Text.GetSubText("000000",1,xx),binval)
    Longbin=text.Append(Longbin,binval)
  EndFor
  xx=8-math.Remainder(text.GetLength(Longbin),8)
  If xx<8 then
    Longbin=text.Append(Longbin,text.GetSubText("00000000",1,xx))
  endif
  Controls.SetTextBoxText(TextBox,longbin)
  longbin = ""
endsub  'ConvertFromBase64
Copyright (c) Microsoft Corporation. All rights reserved.