Microsoft Small Basic
Program Listing:
Embed this in your website
<object id='sbapp' data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='640' height='480'> <param name='source' value='http://smallbasic.com/program/ClientBin/SBWeb.xap'/> <param name='onError' value='onSilverlightError' /> <param name='background' value='white' /> <param name='minRuntimeVersion' value='3.0.40624.0' /> <param name='autoUpgrade' value='true' /> <param name='initParams' value='programId=FTK803' /> </object>
GraphicsWindow
.
Width
=
400
GraphicsWindow
.
Height
=
300
GraphicsWindow
.
Title
=
"Text to Binary : Binary to Text"
TextBox
=
Controls
.
AddMultiLineTextBox
(
10
,
10
)
Controls
.
SetSize
(
TextBox
,
380
,
250
)
ButtonTB
=
Controls
.
AddButton
(
"Convert to Binary"
,
10
,
265
)
ButtonBT
=
Controls
.
AddButton
(
"Convert to Text"
,
140
,
265
)
Controls
.
ButtonClicked
=
onClick
Sub
onClick
LastButton
=
Controls
.
LastClickedButton
If
LastButton
=
"Button1"
Then
ConvertToBinary
(
)
Else
ConvertToText
(
)
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 number
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
Copyright (c) Microsoft Corporation. All rights reserved.