Microsoft Small Basic

Program Listing:
Embed this in your website
GraphicsWindow.Title = "Ternary to decimal : Decimal to ternary"
GraphicsWindow.Width = 500
GraphicsWindow.Height = 300
Box = Controls.AddMultiLineTextBox(10,10)
Controls.SetSize(Box,480,248)
ButtonTernaryToText = Controls.AddButton("Ternary : Text",150,267)
ButtonTextToTernary = Controls.AddButton("Text : Ternary",10,267)
Controls.SetSize(ButtonTernaryToText,130,30)
Controls.SetSize(ButtonTextToTernary,130,30)
Controls.ButtonClicked = ButtonClick
Sub ButtonClick
  ClickedButton = Controls.LastClickedButton
  If ClickedButton = ButtonTernaryToText Then
    Ternary = Controls.GetTextBoxText(Box)
    TernaryToText()
  Else
    Txt = Controls.GetTextBoxText(Box)
    TextToTernary()
  EndIf
EndSub

Sub TernaryToText
  If Ternary < 1 And Text.ConvertToLowerCase(Ternary) <> Text.ConvertToUpperCase(Ternary) Then
    GraphicsWindow.ShowMessage("The input is not an integer","Error")
  ElseIf Text.IsSubText(Text.GetLength(Ternary)/5,".") Then
    GraphicsWindow.ShowMessage("Cannot get text if ternary is not in groups of 5","Error")
  Else
    String = ""
    For i = 1 To Text.GetLength(Ternary)/5
      Value = Text.GetSubText(Ternary,i*5-4,5)
      Total = ""
      For g = Text.GetLength(Value) To 1 Step -1
        Total = Total+Text.GetSubText(Value,g,1)*Math.Power(3,Text.GetLength(Value)-g)
      EndFor
      String = Text.Append(String,Text.GetCharacter(Total))
    EndFor
    Controls.SetTextBoxText(Box,String)
  EndIf
EndSub

Sub TextToTernary
  TernaryValue = ""
  For i = 1 To Text.GetLength(Txt)
    ASCIIValue = Text.GetCharacterCode(Text.GetSubText(Txt,i,1))
    w = 0
    For g = 1 To 5
      w = w + 1
      Remainder = Math.Remainder(ASCIIValue,3)
      ASCIIValue = Math.Floor(ASCIIValue/3)
      Trite[w] = Remainder
    EndFor
    For f = w To 1 Step -1
      TernaryValue = Text.Append(TernaryValue,Trite[f])
    EndFor
  EndFor
  Controls.SetTextBoxText(Box,TernaryValue)
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.