Microsoft Small Basic

Program Listing: RPZ143-2
' Get Character from Unicode
' Copyright (c) 2014 Nonki Takahashi. MIT License.
' Program ID RPZ143-2
'
GraphicsWindow.Title = "Get Character from Unicode"
Form()
clicked = "False"
Controls.ButtonClicked = OnButtonClicked
While "True"
If clicked Then
lastFont = fontName
fontName = Controls.GetTextBoxText(fontBox)
If lastFont <> fontName Then
GraphicsWindow.FontName = fontName
Program.Delay(300)
Controls.Remove(charBox)
charBox = Controls.AddTextBox(80, 120)
EndIf
code = Controls.GetTextBoxText(codeBox)
GetCharFromUnicode()
Controls.SetTextBoxText(charBox, char)
Shapes.SetText(code2Text, code2)
clicked = "False"
Else
Program.Delay(300)
EndIf
EndWhile
Sub OnButtonClicked
clicked = "True"
EndSub
Sub Form
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 10, "Font")
fontBox = Controls.AddTextBox(80, 10 - 4)
fontName = GraphicsWindow.FontName
Controls.SetTextBoxText(fontBox, fontName)
GraphicsWindow.DrawText(10, 40, "Code")
codeBox = Controls.AddTextBox(80, 40 - 4)
code = "U+0041"
Controls.SetTextBoxText(codeBox, code)
GetCharFromUnicode()
code2Text = Shapes.AddText(dec)
Shapes.Move(code2Text, 250, 40)
Controls.AddButton("Get", 10, 70)
GraphicsWindow.DrawText(10, 120, "Character")
Program.Delay(300)
GraphicsWindow.FontSize = 50
charBox = Controls.AddTextBox(80, 120 - 4)
Controls.SetTextBoxText(charBox, char)
EndSub
Sub GetCharFromUnicode
' param code - U+xxxx or decimal for Unicode
' return char - the character for the code
' return code2 - oposite format of the code
If Text.StartsWith(code, "U+") Then
hex = Text.GetSubTextToEnd(code, 3)
Math_Hex2Dec()
code2 = dec
Else
dec = code
Math_Dec2Hex()
len = Text.GetLength(hex)
code2 = "U+" + Text.GetSubTextToEnd("000", len) + hex
EndIf
char = Text.GetCharacter(dec)
EndSub
Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex - hexadecimal text
' return dec - decimal number
dec = 0
iLen = Text.GetLength(hex)
For iPtr = 1 To iLen
dec = dec * 16 + Text.GetIndexOf("0123456789ABCDEF", Text.GetSubText(hex, iPtr, 1)) - 1
EndFor
EndSub
Sub Math_Dec2Hex
' Math | Convert decimal to hexadecimal
' param dec - decimal number
' return hex - hexadecimal text
Stack.PushValue("local", dec)
hex = ""
While 0 < dec
digit = Math.Remainder(dec, 16)
dec = Math.Floor(dec / 16)
hex = Text.Append(Text.GetSubText("0123456789ABCDEF", digit + 1, 1), hex)
EndWhile
If hex = "" Then
hex = "0"
EndIf
dec = Stack.PopValue("local")
EndSub