ASCII (American Standard Code for Information Interchange) is the mapping of the byte values from 0 to 127 to the common printed characters. The values from 0 to 31 are control characters, meaning they coordinate transmission on a teletype. The values from 48 to 57 are the decimal digit characters "0" to "9". The values from 65 to 90 are "A" to "Z", and 97 to 122 are "a" to "z".
Gambas makes dealing with byte value fairly simple. The sample program and its output demonstrate some of the concepts and syntax.
'=============================================================================
Public Sub Main()
'---- Sample string of ordinary characters
Dim theSample As String = "0123 ABCD Gambas Zz"
For p As Integer = 1 To Len(theSample)
Dim theByteValue As Byte = Asc(Mid(theSample, p, 1))
DisplayByteValue(theByteValue)
Next
'---- Some ASCII characters
Print
For d As Integer = 0 To 5
Print d, Chr(48 + d), Chr(64 + d), Chr(96 + d)
Next
'---- Some special characters
Print
Print " Null aka \\0 = "; Asc("\0")
Print " Tab aka \\t = "; Asc("\t"), Asc(gb.Tab)
Print " LF aka \\n = "; Asc("\n"), Asc(gb.Lf)
Print " CR aka \\r = "; Asc("\r"), Asc(gb.Cr)
End
'=============================================================================
Private Sub DisplayByteValue(argByteValue As Byte)
Print Chr(argByteValue); " ";
Print Right("00000000" & Bin(argByteValue), 8); " ";
Print "&"; Right("00" & Hex(argByteValue), 2); "& ";
Print Right(" " & Str(argByteValue), 3); " ";
Print HexSums(argByteValue);
Print BinarySums(argByteValue)
End
'=============================================================================
Private Sub HexSums(argByteValue As Byte) As String
Dim theHighNybble As Byte = Shr(argByteValue, 4)
Dim theLowNybble As Byte = argByteValue And &0F&
Dim r As String = Str(theHighNybble) & " * 16 + " & Str(theLowNybble)
Return Left(r & " ", 16)
End
'=============================================================================
Private Sub BinarySums(argByteValue As Byte) As String
If argByteValue = 0 Then Return "0"
Dim theMaskValue As Integer = 128 ' 100000000b
Dim theResult As String
For b As Integer = 7 To 0 Step -1
If (argByteValue And theMaskValue) > 0 Then
theResult &= " + " & Str(theMaskValue)
End If
' theMaskValue = Shr(theMaskValue, 1)
theMaskValue /= 2
Next
Return Mid(theResult, 4)
End
'=============================================================================
Here is the output:
Code: Select all
0 00110000 &30& 48 3 * 16 + 0 32 + 16
1 00110001 &31& 49 3 * 16 + 1 32 + 16 + 1
2 00110010 &32& 50 3 * 16 + 2 32 + 16 + 2
3 00110011 &33& 51 3 * 16 + 3 32 + 16 + 2 + 1
00100000 &20& 32 2 * 16 + 0 32
A 01000001 &41& 65 4 * 16 + 1 64 + 1
B 01000010 &42& 66 4 * 16 + 2 64 + 2
C 01000011 &43& 67 4 * 16 + 3 64 + 2 + 1
D 01000100 &44& 68 4 * 16 + 4 64 + 4
00100000 &20& 32 2 * 16 + 0 32
G 01000111 &47& 71 4 * 16 + 7 64 + 4 + 2 + 1
a 01100001 &61& 97 6 * 16 + 1 64 + 32 + 1
m 01101101 &6D& 109 6 * 16 + 13 64 + 32 + 8 + 4 + 1
b 01100010 &62& 98 6 * 16 + 2 64 + 32 + 2
a 01100001 &61& 97 6 * 16 + 1 64 + 32 + 1
s 01110011 &73& 115 7 * 16 + 3 64 + 32 + 16 + 2 + 1
00100000 &20& 32 2 * 16 + 0 32
Z 01011010 &5A& 90 5 * 16 + 10 64 + 16 + 8 + 2
z 01111010 &7A& 122 7 * 16 + 10 64 + 32 + 16 + 8 + 2
0 0 @ `
1 1 A a
2 2 B b
3 3 C c
4 4 D d
5 5 E e
Null aka \0 = 0
Tab aka \t = 9 9
LF aka \n = 10 10
CR aka \r = 13 13
Here is a related post from long ago:
viewtopic.php?p=1553