Microsoft Small Basic

Program Listing: KPS036-0
' Text Dump - Hexadecimal Dump for Unicode Text 0.21
' Copyright (c) 2014 Nonki Takahashi. All rights reserved.
'
' History:
' 0.21 2014-03-21 Created from dump 0.1 (KPS036-0)
' 0.1 2014-03-07 Created from tr 0.21. (XWT217)
'
GraphicsWindow.Title = "Text Dump 0.21"
Not = "False=True;True=False;"
WQ = Text.GetCharacter(34)
CR = Text.GetCharacter(13)
LF = Text.GetCharacter(10)
gw = 598
gh = 428
fs = 11
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "LightGray"
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontName = "Verdana"
GraphicsWindow.FontSize = fs
mb = Controls.AddMultiLineTextBox(10, 10)
buf = "ABCDEFG" + CR + LF + "いろはにほへと"
Controls.SetTextBoxText(mb, buf)
bn = Controls.AddButton("Dump", 220, 10)
clicked = "False"
GraphicsWindow.FontName = "Courier New"
Controls.ButtonClicked = OnButtonClicked
While "True"
If clicked Then
buf = Controls.GetTextBoxText(mb)
x = 10
y = 100
Dump()
clicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile
Sub OnButtonClicked
clicked = "True"
EndSub
Sub Dump
' param buf - array of input lines
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
GraphicsWindow.BrushColor = "Black"
scale1 = "Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F"
scale2 = "---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----"
nLine = 0
WriteHeader()
bytes = 0
dec = bytes
Math_Dec2Hex()
AddLeadingZero()
line = hex + " "
len = Text.GetLength(buf)
For i = 1 To len
c = Text.GetSubText(buf, i, 1)
dec = Text.GetCharacterCode(c)
Math_Dec2Hex()
AddLeadingZero()
line = Text.Append(line, hex) + " "
NextByte()
EndFor
r = Math.Remainder(bytes, 16)
If 0 < r Then
nLine = nLine + 1
WriteLine()
dec = bytes
Math_Dec2Hex()
AddLeadingZero()
line = hex + " "
txt = ""
EndIf
line = ""
WriteLine()
EndSub
Sub WriteLine
' param line
' param x, y
GraphicsWindow.DrawText(x, y, line)
y = y + fs
EndSub
Sub WriteHeader
nLine = nLine + 1
line = ""
WriteLine()
nLine = nLine + 1
line = scale1
WriteLine()
nLine = nLine + 1
line = scale2
WriteLine()
EndSub
Sub NextByte
bytes = bytes + 1
If Math.Remainder(bytes, 16) = 0 Then
nLine = nLine + 1
WriteLine()
If Math.Remainder(nLine, 19) = 0 Then
WriteHeader()
EndIf
dec = bytes
Math_Dec2Hex()
AddLeadingZero()
line = hex + " "
txt = ""
EndIf
EndSub
Sub AddLeadingZero
' param hex - original hexadecimal string
' return hex - hexadecimal string with leading zero
If 0 < Math.Remainder(Text.GetLength(hex), 2) Then
hex = Text.Append("0", hex)
EndIf
r = Math.Remainder(Text.GetLength(hex), 4)
hex = Text.Append(Text.GetSubText("000", 1, r), hex)
EndSub
Sub Math_Dec2Hex
' Math | Convert decimal to hexadecimal
' param dec - decimal number
' returns hex - hexadecimal string
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