VB.NET•Add Supporting Functions

Listing 1. This module contains all the supporting functions you need to convert an SMS message to the PDU format and vice versa.

Module Module1
   Function OctetsToSeptets(ByVal hexStr As _
      String) As String
'===converts from 8 bits to 7 bits===  
      Dim i As Integer
      Dim bitStr As String
      For i = 1 To (hexStr.Length) Step 2
         bitStr &= hexToBits(Mid(hexStr, _
            i + 1, 1))
         bitStr &= hexToBits(Mid(hexStr, i, 1))
      Next
      'chop into 7 bits block, and convert
      Dim result As String
      For i = 1 To (bitStr.Length) Step 7
         result &= ConvertToChar(Mid(bitStr, _
            i, 7))
      Next
      Return result
   End Function
   Function ConvertToChar_
      (ByVal bitStr As String) As Char
'===converts a bit string to a character===  
      Dim i, value As Integer
      'pad with zeros
      For i = bitStr.Length + 1 To 7
         bitStr = "0" & bitStr
      Next
      For i = 0 To 6
         value += Mid(bitStr, i + 1, 1) * _
            Math.Pow(2, i)
      Next i
      Return Chr(value)
   End Function
   Public Function septetsToOctets(ByVal str _
      As String) As String
'===converts from 7 bits to 8 bits===  
      Dim i As Integer
      Dim charByte(160) As Byte
      Dim octetString As String
      Dim hexString As String
      For i = 0 To str.Length - 1
         charByte(i) = Asc(Mid(str, i + 1, 1))
         octetString &= byteToBits(charByte(i))
      Next
      For i = 1 To (octetString.Length) Step 8
         hexString &= bitsToHex(Mid _
            (octetString, i + 4, 4))
         hexString &= bitsToHex(Mid _
            (octetString, i, 4))
      Next
      Return (hexString)
   End Function
   Function byteToBits(ByVal num As Byte) _
      As String
'===converts byte to bit in reverse order===  
      Dim result As String
      While (num / 2 <> 0)
         result &= (num Mod 2)
         num = (num \ 2)
      End While
      If num = 1 Then
         result = "1" + result
      End If
      'pad with "0" in front
      Dim i As Integer
      For i = result.Length + 1 To 7
         result &= "0"
      Next
      Return result
   End Function
   Function bitsToHex(ByVal str As String) _
      As Char
'===converts bits to hexadecimal===  
      Dim result As Integer
      Dim i As Integer
      For i = str.Length + 1 To 4
         str &= "0"
      Next
      result = Mid(str, 1, 1) * 1
      result += Mid(str, 2, 1) * 2
      result += Mid(str, 3, 1) * 4
      result += Mid(str, 4, 1) * 8
      Select Case result
         Case 0 : Return "0"
         Case 1 : Return "1"
         Case 2 : Return "2"
         Case 3 : Return "3"
         Case 4 : Return "4"
         Case 5 : Return "5"
         Case 6 : Return "6"
         Case 7 : Return "7"
         Case 8 : Return "8"
         Case 9 : Return "9"
         Case 10 : Return "A"
         Case 11 : Return "B"
         Case 12 : Return "C"
         Case 13 : Return "D"
         Case 14 : Return "E"
         Case 15 : Return "F"
      End Select
   End Function
   Function hexToBits(ByVal hexChar As Char) _
      As String
'===converts hex to bits in reverse order===
      Select Case hexChar
         Case "0" : Return "0000"
         Case "1" : Return "1000"
         Case "2" : Return "0100"
         Case "3" : Return "1100"
         Case "4" : Return "0010"
         Case "5" : Return "1010"
         Case "6" : Return "0110"
         Case "7" : Return "1110"
         Case "8" : Return "0001"
         Case "9" : Return "1001"
         Case "A" : Return "0101"
         Case "B" : Return "1101"
         Case "C" : Return "0011"
         Case "D" : Return "1011"
         Case "E" : Return "0111"
         Case "F" : Return "1111"
      End Select
   End Function
   Public Function FormatPhone(ByVal num _
      As String) As String
'===reverses phone #; swap alternate digits===
'e.g. 12345678 becomes 21436587
      Dim i As Integer
      Dim numBA As Byte()
      numBA = System.Text.Encoding.ASCII. _
         GetBytes(num)
      For i = 0 To num.Length - 1 Step 2
         Dim temp As Byte = numBA(i).ToString
         numBA(i) = numBA(i + 1).ToString
         numBA(i + 1) = temp
      Next
      Return ByteArrayToString(numBA)
   End Function
   Function ByteArrayToString(ByVal BA() _
      As Byte) As String
'===converts byte array to string===
      Dim b As Byte
      Dim InBu As String
      Dim enc As System.Text.ASCIIEncoding
      enc = New System.Text.ASCIIEncoding
      InBu = enc.GetString(BA, 0, BA.Length())
      Return InBu
   End Function
End Module