I am not really that good with vb.net, but i am facing a problem.
I have to convert a string into binary (thing i have done) but after that, I have to encrypt the string by combining it whith another string, also converted into binary.
Problem : I am broswing this result whith the help of a string y() , but the result has the dimension bigger that 32 bit and i get the error : {"Index was outside the bounds of the array."}
how cand i solve this?
Thanks
Dim preluare As String = TextBox5.Text ' the binary text i have to encrypt
Dim inter() As Char = preluare.ToCharArray
Dim b As Integer = TextBox5.Text.Count
Dim x As String = TextBox15.Text ' the key of encryption
Dim y() As Char = x.ToCharArray
Dim bb As Integer = TextBox15.Text.Count
' the key has to be the same lenght as the initial text
If b > x.Count Then
While x.Count < b
x = x + x
End While
End If
If x.Count > b Then
Dim w As Integer = x.Count
x = x.Trim().Substring(w - b)
End If
' xor operation
For i As Integer = 0 To b - 1
If inter(i) = "0" Then
'MsgBox(i)
If y(i) = "0" Then ' THIS IS WHERE I GET THE ERROR
TextBox10.Text = TextBox10.Text + "0"
Else
TextBox10.Text = TextBox10.Text + "1"
End If
Else
If inter(i) = "1" Then
If y(i) = "0" Then
TextBox10.Text = TextBox10.Text + "1"
Else
TextBox10.Text = TextBox10.Text + "0"
End If
End If
End If
Next
If you're sure the error was because size of y() bigger than 32 bit integer, how about using 64 bit integer data type as counter :
For i As Int64 = 0 To b - 1
.......
.......
Next
you can use unsigned variables like
uint16, uint32...
Related
I have SHA-2 hash values. Here's an example:
qlbQbEd8khe2vEYG9acKScUiVTC6y1UorkMvptATwwxkVApkOCUH7hwkncbi2TY78HrIeC19G8EHlaAmj6sBAwCxhF2TeOpmJ1+2OfbfXF+jMWUO74O7WHJuwoq+R5aKa0c2QYbyrcd/DWSprdkrF1gyz+RWVXYQug63aAhC0j0=
I need to convert these into an alphanumeric hash — that is, using only capital letters and numbers ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"). The process has to be reversible for SHA-2 hash comparison.
The reason for this conversion is aesthetics. The resulting hash will be used as a license key, and has to be shorter and more easily readable and typeable.
I read that I could try to re-encode that hash into Base36, but I have been unsuccessful. Can anyone give any suggestions?
This code is an example for converting Base36 to/from Base26
Const Base36 As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const Base26 As String = "abcdefghijklmnopqrstuvwxyz"
Function Num2Base(Vlu As Long, Base As String) As String
Dim A() As Integer = Nothing
Dim y As Integer = -1
Dim X As Long = Vlu
Dim Ln As Integer = Base.Length
Do While X > -1
y = y + 1
ReDim Preserve A(y)
A(y) = X Mod Ln
X = ((X - A(y)) / Ln) - 1
Loop
Dim res As String = ""
If y >= 0 Then
For Each d As Integer In A
res &= Mid(Base, d + 1, 1)
Next
End If
Return res
End Function
Function Base2Num(s As String, Base As String) As Long
If s.Length = 0 Then
Throw New Exception("'S' can not be empty")
End If
Dim res As Long = -1
Dim Ln As Integer = Base.Length
Dim x As Integer = 0
For Each c In s
res += (Base.IndexOf(c) + 1) * (Ln ^ x)
x += 1
Next
Return res
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Num As Long = 1313
Dim b36 As String = Num2Base(Num, Base36) 'convert Num to base36
Dim b26 As String = Num2Base(Num, Base26) 'convert Num to base26
MessageBox.Show("Num:" & Num & vbCrLf & "ToBase36:" & b36 & vbCrLf & "ToBase26:" & b26, "Message 1")
Dim N36 As Long = Base2Num(b36, Base36) 'convert Base36 to Num
Dim N26 As Long = Base2Num(b26, Base26) 'convert Base26 to Num
MessageBox.Show("N36:" & N36 & vbCrLf & "N26:" & N26, "Message 2")
b26 = Num2Base(Base2Num(b36, Base36), Base26) 'convert Base36 to Base 26
MessageBox.Show("Base36 to Base 26 :" & b26, "Message 3")
b36 = Num2Base(Base2Num(b26, Base26), Base36) 'convert Base26 to Base 36
MessageBox.Show("Base26 to Base 36 :" & b36, "Message 4")
MessageBox.Show(Num2Base(1486154465904653, Base36), "Message 5")
End Sub
Unfortunately this code can not work with long strings(more than 12 chrs) due the limitation of the type Long,so you may need to split SHA-2 string to small parts
If I manually put my address in for EmailMessage.To.Add(GetDelimitedField(x, strEmailRep, ";")) It sends me the message just fine. However If I use the code as is below which is using a list that looks like ;email1#mail.com;email2.mail.com
Then it gives an error that email address cannot be blank
Somewhere in GetDelimitedField is erasing addresses. I'm not sure where the problem is actually occurring. Here is all the code involved with this.
strmsg = "LOW STOCK ALERT: Component (" & rsMPCS("MTI_PART_NO") & ") has reached or fallen below it's minimum quantity(" & rsMPCS("MIN_QTY") & ")."
Dim EmailMessage As MailMessage = New MailMessage
EmailMessage.From = New MailAddress("noreply#mail.com")
For x = 1 To GetCommaCount(strEmailRep) + 1
EmailMessage.To.Add(GetDelimitedField(x, strEmailRep, ";"))
Next
EmailMessage.Subject = ("LOW STOCK ALERT!")
EmailMessage.Body = strmsg
EmailMessage.Priority = MailPriority.High
EmailMessage.IsBodyHtml = True
Dim smtp As New SmtpClient("smtp.mycompany.com")
smtp.UseDefaultCredentials = True
smtp.Send(EmailMessage)
Public Function GetCommaCount(ByVal sText As String)
Dim X As Integer
Dim Count As Integer
Dim Look As String
For X = 1 To Len(sText)
Look = Microsoft.VisualBasic.Left(sText, X)
If InStr(X, Look, ";", 1) > 0 Then
Count = Count + 1
End If
Next
GetCommaCount = Count
End Function
Public Function GetDelimitedField(ByRef FieldNum As Short, ByRef DelimitedString As String, ByRef Delimiter As String) As String
Dim NewPos As Short
Dim FieldCounter As Short
Dim FieldData As String
Dim RightLength As Short
Dim NextDelimiter As Short
If (DelimitedString = "") Or (Delimiter = "") Or (FieldNum = 0) Then
GetDelimitedField = ""
Exit Function
End If
NewPos = 1
FieldCounter = 1
While (FieldCounter < FieldNum) And (NewPos <> 0)
NewPos = InStr(NewPos, DelimitedString, Delimiter, CompareMethod.Text)
If NewPos <> 0 Then
FieldCounter = FieldCounter + 1
NewPos = NewPos + 1
End If
End While
RightLength = Len(DelimitedString) - NewPos + 1
FieldData = Microsoft.VisualBasic.Right(DelimitedString, RightLength)
NextDelimiter = InStr(1, FieldData, Delimiter, CompareMethod.Text)
If NextDelimiter <> 0 Then
FieldData = Microsoft.VisualBasic.Left(FieldData, NextDelimiter - 1)
End If
GetDelimitedField = FieldData
End Function
You can split the list easier using string.Split:
Dim strEmails = "a#test.com;b#test.com;c#test.com;"
Dim lstEmails = strEmails.Split(";").ToList()
'In case the last one had a semicolon:
If (lstEmails(lstEmails.Count - 1).Trim() = String.Empty) Then
lstEmails.RemoveAt(lstEmails.Count - 1)
End If
If (lstEmails.Count > 0) Then
lstEmails.AddRange(lstEmails)
End If
Select Case dlg
Case Windows.Forms.DialogResult.Yes
If TextBox12.Text = "" Then
Dim a, b, c As Integer
a = TextBox7.Text
b = TextBox8.Text //my problem
c = TextBox11.Text
TextBox12.Text = a + b - c
End If
If TextBox6.Text = "" Then
TextBox6.Text = "-"
End If
I don't know how to fix this error:
Conversion from string "" to type 'Integer' is not valid
Try using Integer.Parse:
Select Case dlg
Case Windows.Forms.DialogResult.Yes
If TextBox12.Text = "" Then
Dim a, b, c As Integer
a = Integer.Parse(TextBox7.Text)
b = Integer.Parse(TextBox8.Text) //my problem
c = Integer.Parse(TextBox11.Text)
TextBox12.Text = a + b - c
End If
If TextBox6.Text = "" Then
TextBox6.Text = "-"
End If
If Not String.IsNullOrEmpty(TextBox8.Text) Then
b = Integer.Parse(TextBox8.Text)
End If
You can check if the textbox is empty or null and then use int parse.
Or you could use Pikoh's suggestion of using Int32.TryParse
Int32.TryParse Method
You should look at using Integer.TryParse. The advantage of using TryParse is it won't throw an exception should the conversion fail:
Dim a As Integer = 0
Dim b As Integer = 0
Dim c As Integer = 0
Integer.TryParse(TextBox7.Text, a)
Integer.TryParse(TextBox8.Text, b)
Integer.TryParse(TextBox11.Text, c)
TextBox12.Text = (a + b - c).ToString()
You should also look at setting Option Strict On:
Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.
This will help you write better code in the long run.
I have written the following program in order to add "1" to the code we assign mail that comes into the office. The user puts in input on a form and the query brings back the last code assigned for the state and company. The program should increase that number by 1. Example TN08801401 (TN - COCODE 0880 Year - 14 - Sequence 01) should now be TN08801402.
The program worked fine as long as the query brought a result. If the answer was null (meaning that it was the first state/company/mail we had, then I wanted the program to create the code. Example WA08801501.
The problem is that it only giving me the errorsub routine instead of the base program.
Thanks,
Here is the code as follows:
Private Sub form_load()
Dim qryrslt As String
Dim State As String
Dim num As String
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As String
Dim total As String
Dim CoNo As String
Dim yearseq As String
On Error GoTo errorsub
qryrslt = DLookup("[idkey]", "assignment_qry")
State = Left(qryrslt, 6)
num = Right(qryrslt, 4)
If IsNumeric(num) Then
num1 = CInt(num)
Else
num1 = 0
End If
num2 = num1 + 1
num3 = CStr(num2)
total = State + num3
Me.TxtIdKey = total
errorsub:
State = Forms!assignment_form!CmbState
CoNo = Forms!assignment_form!CmbCompany
yearseq = 1501
total = State + CoNo + yearseq
Me.TxtIdKey = total
It is always simpler to concatenate fields together, rather than split them apart. It appears that your 'idkey' is a single field made up of multiple values. You would be better off using a separate field for each value and concatenating them as needed. Let's say your table is tblPrograms. Fields would be ProgState (text), ProgCode (text), ProgYear(integer), ProgSequence (integer). Anywhere you need it, you can concatenate ProgState & "-" & ProgCode & ProgYear & ProgSequence. To get the next sequence is a simple lookup.
Dim intNewSequence as Integer
intNewSequence = Nz(DMax("ProgSequence", "tblPrograms", "ProgState ='" & Forms!assignment_form!CmbState & "' AND ProgCode = '" & Forms!assignment_form!CmbCompany & "' AND ProgYear = " & Forms!assignment_form!ProgYear), 0) + 1
Additionally, this makes any queries that would use any of those fields in a where clause, much more efficient.
I think you need to need to do a null check before you assign State & num. Sorta like this:
if (qryslt is not null)
{State = Left(qryrslt, 6)
num = Right(qryrslt, 4)
If IsNumeric(num) Then
num1 = CInt(num)
Else
num1 = 0
End If
num2 = num1 + 1
num3 = CStr(num2)
total = State + num3
Me.TxtIdKey = total}
Sorry if the syntax isnt right for vb - it's been a while, but I hope you get the idea.
I split the add into two steps to make this work. Step 1 was to find the last in sequence or create a default:
Private Sub Command17_Click()
Dim Qry As String
Dim State As String
Dim num As String
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As String
Dim total As String
Dim CoNo As String
Dim yearseq As String
Qry = "" & DLookup("IDKEY", "Assignment_qry")
If Qry = "" Then
State = Forms!assignment_form!CmbState
CoNo = Forms!assignment_form!CmbCompany
yearseq = 1500
total = State + CoNo + yearseq
Me.TxtAssignment = total
Else
Me.TxtAssignment = Qry
End If
End Sub
Then I added one in another step:
Private Sub form_load()
Dim qryrslt As String
Dim State As String
Dim num As String
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As String
Dim total As String
Dim CoNo As String
Dim yearseq As String
qryrslt = Forms!assignment_form!TxtAssignment
State = Left(qryrslt, 6)
num = Right(qryrslt, 4)
If IsNumeric(num) Then
num1 = CInt(num)
Else
num1 = 0
End If
num2 = num1 + 1
num3 = CStr(num2)
total = State + num3
Me.TxtIdKey = total
Me.TxtCompany = Mid(State, 3, 5)
Me.TxtCoName = DLookup("Name", "Newentry_coname_qry")
I would like to compare two strings in a vb.net windows application
Imports System.Windows
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As String = "$99"
Dim y As String = "$9899"
If s > y Then
MessageBox.Show("Hi")
End If
End Sub
End Class
Could anyone one correct the logic if there is any mistake in that?
You are comparing strings, not integers.
You could compare them as integers by replacing "$" with "" and then convert it to an integer.
Replace the $ to ""
s = s.Replace("$", "");
y = y.Replace("$", "");
Convert both of them to integers
Dim result1 As Integer
Dim result2 As Integer
result1 = Convert.ToInt32(s)
result2 = Convert.Toint32(y);
Then you can do
if (result1 > result2) { ... }
Dim sum1 As Int32 = 99
Dim sum2 As Int32 = 9899
'this works as expected because you are comparing the two numeric values'
If sum1 > sum1 Then
MessageBox.Show("$" & sum1 & " is greater than $" & sum2)
Else
MessageBox.Show("$" & sum2 & " is greater than $" & sum1)
End If
'if you really want to compare two strings, the result would be different than comparing the numeric values'
'you can work around this by using the same number of digits and filling the numbers with leading zeros'
Dim s As String = ("$" & sum1.ToString("D4")) '$0099'
Dim y As String = ("$" & sum2.ToString("D4")) '$9899'
If s > y Then
MessageBox.Show(s & " is greater than " & y)
Else
MessageBox.Show(y & " is greater than " & s)
End If
I recommend always to use Integers for numeric values, particularly if you want to compare them. You can format the values as string after you compared the numeric values.
What do you mean compare by length or content?
dim result as string
dim s as string = "aaa"
dim y as string = "bbb"
if s.length = y.length then result = "SAME" '= true
if s = y then result = "SAME" '= false
MessageBox.Show(result)