ROT13. The ROT13 algorithm encodes plain text. In this algorithm, the letters A-Z and a-z are rotated 13 places. This function can be written in the VB.NET language.
Some notes. ROT13 scrambles the text but is easily reversible. It can be implemented with If and ElseIf in VB.NET code—this is likely the simplest approach.
Example. We test to make sure the Rot13 function, when called twice, correctly reverses the operation. In the Rot13 function, we receive one String parameter and return a new String value.
Module Module1
Sub Main()
' Use Rot13.
Console.WriteLine("The apartment")
Console.WriteLine(Rot13("The apartment"))
Console.WriteLine(Rot13(Rot13("The apartment")))
End Sub
Public Function Rot13(ByVal value As String) As String
' Could be stored as integers directly.
Dim lowerA As Integer = Asc("a"c)
Dim lowerZ As Integer = Asc("z"c)
Dim lowerM As Integer = Asc("m"c)
Dim upperA As Integer = Asc("A"c)
Dim upperZ As Integer = Asc("Z"c)
Dim upperM As Integer = Asc("M"c)
' Convert to character array.
Dim array As Char() = value.ToCharArray
' Loop over string.
Dim i As Integer
For i = 0 To array.Length - 1
' Convert to integer.
Dim number As Integer = Asc(array(i))
' Shift letters.
If ((number >= lowerA) AndAlso (number <= lowerZ)) Then
If (number > lowerM) Then
number -= 13
Else
number += 13
End If
ElseIf ((number >= upperA) AndAlso (number <= upperZ)) Then
If (number > upperM) Then
number -= 13
Else
number += 13
End If
End If
' Convert to character.
array(i) = Chr(number)
Next i
' Return string.
Return New String(array)
End Function
End ModuleThe apartment
Gur ncnegzrag
The apartment
A summary. The ROT13 algorithm is easily broken and won't stop others from reading your important secrets. ROT13 is helpful for learning how to write string-manipulation code.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 18, 2021 (image).