Simple rot13 encoder in vb.net - vb.net

I am looking for a simple way to encode an inputted text into Rot13. I am hitting a brick wall at the stage of being able to separate out words into individual characters and integers so that I can change each one and output the result. I can do it with single letters using a simple if statement listed bellow but if anyone can help with a way of doing it for whole words I would be very appreciative.
If kInput = "a" then kOutput = "n"
Thanks, Kai

Looks like people are giving good answers to this but here's my try at it.
Dim input As String = "This is a Test!! Guvf vf n Grfg!!"
Dim result As StringBuilder = New StringBuilder()
For Each ch As Char In input
If (Not Char.IsLetter(ch)) Then
result.Append(ch)
Continue For
End If
Dim checkIndex As Integer = Asc("a") - (Char.IsUpper(ch) * -32)
Dim index As Integer = ((Asc(ch) - checkIndex) + 13) Mod 26
result.Append(Chr(index + checkIndex))
Next
Console.WriteLine(result.ToString())
EDIT: improved to remove need for uppercase check. This will properly handle case and special characters with only 1 if statement inside the loop.

It seems like you're making this way harder than it has to be. No need to separate words, etc, and definitely no need for a large If/Else block:
Public Function Rot13(ByVal input As String) As String
Dim result As Char() = input.ToCharArray()
For i As Integer = 0 To result.Length - 1
Dim temp As Integer = Asc(result(i))
Select Case temp
Case 65 to 77, 97 To 109 'A - M
result(i) = Chr(temp + 13)
Case 78 to 90, 110 To 122 'N - Z
result(i) = Chr(temp - 13)
End Select
Next i
Return New String(result)
End Function
Note that this was entered directly into the browser window and is completely untested.

Just call it once to encode, call it again to decode.
Private Function ROT13_Encode(ByVal Input As String) As String
Dim chrs As Char() = Input.ToCharArray()
Dim ReturnString As String = ""
Dim CharInt As Integer
For Each Chr As Char In chrs
CharInt = Asc(Chr)
If CharInt >= 65 And CharInt <= 77 Then 'A-M
CharInt += 13
ElseIf CharInt >= 78 And CharInt <= 90 Then 'M-Z
CharInt -= 13
ElseIf CharInt >= 97 And CharInt <= 109 Then 'a-m
CharInt += 13
ElseIf CharInt >= 110 And CharInt <= 122 Then 'm-z
CharInt -= 13
End If
ReturnString &= ChrW(CharInt)
Next
Return ReturnString
End Function

Related

Convert big string to decimal in vb.net

Hello i have big string value which is md5 of something now i need to convert it into the decimal value
for example
Dim md5_s As String = "6F05AF42533432A5513610FE839ACC86"
now i need output same like the online converters to this
"54 70 48 53 65 70 52 50 53 51 51 52 51 50 65 53 53 49 51 54 49 48 70
69 56 51 57 65 67 67 56 54 "
is it possible i don't want spaces in the above converted decimal ?
vb.net help please
Okay here i tried and got it n is my approach works fine will this fine n work always right
Dim t As String
Dim a As String = "6F05AF42533432A5513610FE839ACC86"
For Each c As Char In a
t &= Convert.ToInt32(c)
Next
TextBox1.Text = t
will this one is right ?
result is same what i am looking for as
5470485365705250535151525150655353495154494870695651576567675654
so i assume this is right huh ?
I'm not quite sure this is what you are really looking for but this is what you asked for
For count = 0 To md5_s.Length - 1
Dim tempChar As String = md5_s.Substring(count, 1)
Console.Write(Asc(tempChar))
Next
What is more likely what you want is something like this
Private Function HexToByteArray(ByVal hex As [String]) As Byte()
Dim NumberChars As Integer = hex.Length
Dim bytes As Byte() = New Byte(NumberChars / 2 - 1) {}
For i As Integer = 0 To NumberChars - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
either way ... hope this helps

Automatic Calculation with given numbers

I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)

Generate random string in text field

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.
I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:
Private Sub command133_Click()
End Sub
This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.
Sub test()
Dim s As String * 8 'fixed length string with 8 characters
Dim n As Integer
Dim ch As Integer 'the character
For n = 1 To Len(s) 'don't hardcode the length twice
Do
ch = Rnd() * 127 'This could be more efficient.
'48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
Next
Debug.Print s
End Sub
Try this function:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Workin on #Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.
Code :
Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String
Dim random As String ' * 8 'fixed length string with 8 characters
Dim j As Integer
Dim ch As Integer ' each character
random = ""
For j = 1 To numberOfCharacters
random = random & GenerateRandomAlphaNumericCharacter
Next
GenerateUniqueSequence = random
End Function
Public Function GenerateRandomAlphaNumericCharacter() As String
'Numbers : 48 is '0', 57 is '9'
'LETTERS : 65 is 'A', 90 is 'Z'
'letters : 97 is 'a', 122 is 'z'
GenerateRandomAlphaNumericCharacter = ""
Dim i As Integer
Randomize
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
Randomize
Select Case i
Case 1 'Numbers
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
Case 2 'LETTERS
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
Case 3 'letters
GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
End Select
End Function
I use it with random number of characters, like this :
'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)
Result :
s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20
NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.
Normally distribution will look like:
10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars.
Numbers will normally be 10/62 parts of the string or 1/6.2
With the code
i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories
the count of numbers is pushed up to 1/3 (on average)
Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.

VBA How do i split an integer into separate parts

I'm working in VBA within Excel.
I need to split the integer into two parts, specifically the first two digits and the last two digits.
The numbers have a maximum of four digits and at least one. (I've already sorted out the blank values) eg.
7 should become 0 and 7,
23 should become 0 and 23,
642 should become 6 and 42,
1621 should become 16 and 21.
This is the code I have so far
Function Bloog(value1 As Integer)
Dim value1Hours, value1Mins As Integer
Select Case Len(value1) 'gives a number depending on the length of the value1
Case 1, 2 ' e.g., 2 = 0, 2 or 16 = 0, 16
value1Hours = 0
value1Mins = value1
Case 3 ' e.g., 735 = 7, 35
value1Hours = Left(value1, 1) ' 7
value1Mins = Right(value1, 2) ' 35
Case 4 ' e.g., 1234 = 12, 34
value1Hours = Left(value1, 2) ' 12
value1Mins = Right(value1, 2) ' 34
End Select
However when go to get the values i find that they have not been split up into the separate parts as the Left() and Right() function would have me believe.
Len() doesn't appear to be working either, when it was given the value 723 it returned a length of 2.
Any tips would be appreciated.
=======================================
After a suggestion I've cast the values as strings then done the case statement and converted them back afterwards. (because I need them for some calculations)
Private Function Bloog(value1 As Integer) As Integer
Dim strValue1 As String
Dim strValue1Hours, strValue1Mins As String
Dim value1Hours, value1Mins As Integer
'converts the values into strings for the Left() and Right() functions
strValue1 = CStr(value1)
Select Case Len(value1) 'gives a number depending on the length of the value1
Case 1, 2 ' e.g., 2 = 0, 2 or 16 = 0, 16
strValue1Hours = 0
strValue1Mins = value1
Case 3 ' e.g., 735 = 7, 35
strValue1Hours = Left(value1, 1) ' 7
strValue1Mins = Right(value1, 2) ' 35
Case 4 ' e.g., 1234 = 12, 34
strValue1Hours = Left(value1, 2) ' 12
strValue1Mins = Right(value1, 2) ' 34
End Select
value1Hours = CInt(strValue1Hours)
value1Mins = CInt(strValue1Mins)
Len() still believes that the length of the string is 2 and so case 2 statement was triggered, despite this the the strValue1Mins and the value1Mins still equals 832.
=======================
Len() was testing for Value1 not strValue1, everything works fine after that.
value1 is an integer, why not use arithmetic operations ?
Dim value1Hours as Integer,value1Mins as Integer
value1Mins = value1 Mod 100
value1Hours = Int(value1 / 100)
Hope this helps. This is the most simple way that I could think of.
ValueAsString = Right("0000" & value1,4)
strValue1Hours = Left(ValueAsString, 2)
strValue1Mins = Right(ValueAsString, 2)
For what it's worth:
value1Hours = CInt(Left(Format(x, "0000"), 2))
value1Mins = CInt(Right(Format(x, "0000"), 2))

Multiple select cases in VB.NET

I have tried the below:
Select Case Combo1.SelectedItem Or Combo2.SelectedItem
But I get the error:
Conversion from String "string here" to type 'Long' is not valid
Is it possible to have multiple select cases?
You separate multiple values by using a comma:
Case Combo1.SelectedItem, Combo2.SelectedItem
Using Or would make it an expression that would be evaluated before compared to the value in the Select.
If your value in the Select is a Long value, then you may need to convert the strings from the controls:
Case CLng(Combo1.SelectedItem), CLng(Combo2.SelectedItem)
To address the question directly, using multiple values as a test expression in a select is not possible:
Select Case v1, v2 'Not possible
Hi Googled and saw this question without an answer. Upon further research, I found this to work for my purposes.
Basically, you start with:
Select case True
Then each case statement can be combinations of the two variables. When both are met, the case is true and will execute the appropriate code.
https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim value As Integer
For i = 1 To 3
For j = 1 To 5
value = (GetCode(i, j))
TextBox1.Text = TextBox1.Text & "i=" & i & "->j=" & j & "=" & value & vbCrLf
Next
Next
End Sub
Function GetCode(ByVal v1 As Integer, ByVal v2 As Integer) As Integer
Dim retval As Integer
Dim forselect As String
forselect = v1 & v2
Select Case forselect
Case 11
retval = 11
Case 12
retval = 12
Case 13
retval = 13
Case 14
retval = 14
Case 15
retval = 15
Case 21
retval = 21
Case 22
retval = 22
Case 23
retval = 23
Case 24
retval = 24
Case 25
retval = 25
Case 31
retval = 31
Case 32
retval = 32
Case 3, 3
retval = 33
Case 34
retval = 34
Case 35
retval = 35
End Select
Return retval
End Function