1: Imports System.Text
2:
3: Public Class USSDDecoder
4: ' USSD USCS2 PDU Decoder By Billy Riantono - Batu Kajang 2012
5:
6: Public Shared Function DecodeUSCS2Text(ByVal s_str_string As String) As String
7: Dim result_prefix As String = ""
8: Dim NewString As String = ""
9: Dim i As Integer
10: For i = 0 To s_str_string.Length - 1
11: If (MakeNum(s_str_string.Substring(i, 1)) <> 16) Then
12: NewString += s_str_string.Substring(i, 1)
13: End If
14: Next
15: s_str_string = NewString
16: Dim i_int_length = s_str_string.Length
17: If (i Mod 2) Then
18: Return "Error : Length is Not Even"
19: End If
20: Dim messageLength = s_str_string.Length / 2
21: Dim buffer = GetUserMessage16(0, s_str_string, messageLength)
22: Return buffer
23: End Function
24: #Region "Private FUnction"
25:
26: Private Shared Function HexToNum(ByVal numbers As String) As String
27: Dim tens = MakeNum(numbers.Substring(0, 1))
28: Dim ones = "0"
29: Dim strNumber As String = Convert.ToString(numbers)
30: If (strNumber.Length > 1) Then
31: ones = MakeNum(numbers.Substring(1, strNumber.Length - 1))
32: End If
33: If (Convert.ToString(ones) = "X") Then
34: Return "00"
35: End If
36: Return Convert.ToString((tens * 16) + (ones * 1))
37: End Function
38: Private Shared Function GetUserMessage16(ByVal skip_char As Integer, ByVal Input As String, ByVal truelength As Integer)
39: Dim smsMessage = ""
40: Dim charCounter As Integer = 0
41: Dim i As Integer = 0
42: While i < Input.Length
43: Dim EndLength1 = i + 2
44: Dim EndLength2 = i + 4
45: Dim hex1 = Input.Substring(i, EndLength1 - i)
46: Dim hex2 = Input.Substring(i + 2, EndLength2 - (i + 2))
47: charCounter += 1
48: If charCounter > skip_char Then
49: smsMessage += "" & Character(Convert.ToInt32(Convert.ToInt32(HexToNum(hex1)) * 256 + Convert.ToInt32(HexToNum(hex2))))
50: End If
51: i = i + 4
52: End While
53: Return smsMessage
54: End Function
55: Private Shared Function Character(ByVal Num As Long, Optional ByVal Special As Integer = 256) As String
56: 'This Function from http://www.freevbcode.com/ShowCode.asp?ID=8541
57: 'Special must not be higher then 256 or lower then 0
58: If Special < 1 Then
59: Num = 1
60: ElseIf Special > 256 Then
61: Num = 256
62: End If
63:
64: 'If passed Num is higher then 255, it keeps subtracting
65: 'the Num by Special(256) until Num becomes legal ASCII number.
66: If Num > 255 Then
67: While Num > 255
68: Num = Num - Special
69: End While
70: End If
71:
72: 'If Num is lower then 0, it keeps adding Special(256)
73: 'until Num becomes legal ASCII number.
74: If Num < 0 Then
75: While Num < 0
76: Num = Num + Special
77: End While
78: End If
79:
80: 'At the end it just passes the Num to Chr() function
81: Return Chr(Num)
82:
83: End Function
84:
85:
86: Private Shared Function MakeNum(ByVal str As String) As Integer
87:
88: If Not (str = "0" Or str = "1" Or str = "2" Or str = "3" Or str = "4" Or str = "5" Or str = "6" Or str = "7" Or str = "8" Or str = "9") Then
89: Select Case UCase(str)
90: Case "A"
91: Return 10
92: Case "B"
93: Return 11
94: Case "C"
95: Return 12
96: Case "D"
97: Return 13
98: Case "E"
99: Return 14
100: Case "F"
101: Return 15
102: Case Else
103: Return 16
104: End Select
105: End If
106: If ((Convert.ToInt32(str) >= 0) And (Convert.ToInt32(str) <= 9)) Then
107: Return Convert.ToInt32(str)
108: End If
109: End Function
110: #End Region
111: End Class