Hi here is a place I will post some of my small code snippets that maybe helpful to people.
commands and suggestions on the code snippets welcome.
Snippet-1 Encode string to hexadecimal, decode string from hexadecimal.
vbnet Code:
Public Function EncodeDecodeStr(ByVal Source As String, ByVal Encode As Boolean) As String Dim sb As New System.Text.StringBuilder() If Encode Then 'Encode to hexadecimal string. For Each c As Char In Source sb.Append(Asc(c).ToString("x2")) Next c Else 'Decode from hexadecimal string. For x As Integer = 0 To Source.Length - 1 Step 2 sb.Append(Chr(Val("&H" & Source.Substring(x, 2)))) Next x End If 'Return new string. Return sb.ToString() End Function
Example.
vbnet Code:
Dim s As String = "VBForums is the best forum on the net." Dim b As String = EncodeDecodeStr(s, True) 'Encode to hex str MessageBox.Show("Encoded: " & b, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information) 'Show decoed hex str MessageBox.Show("Decoded: " & EncodeDecodeStr(b, False), "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)


Reply With Quote