VB.NET Performing XOR On Decimal Values - vb.net

I'm porting some code from php to vb.net, but I stuck
on the XOR function. I cannot perform the XOR when one of the
values is greater than integer datatype range.
Here is my testing code :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim A As Integer = 1254378503
Dim B As Integer = 3058210502 'Overflow here
Dim SIGN1 = A Xor B
Dim SIGN2 = EncodeBase64(SIGN1)
MsgBox(SIGN2)
End Sub
Public Function EncodeBase64(ByVal input As String) As String
Return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input))
End Function
End Class
I've been browsing through other posts regarding my question, but honestly
I cannot find any solution or simply cannot understand it. The basic question is how do I perform XOR on decimal datatype (greater then integer) just like php does.
Any help or hint on how to achieve it will be much apreciated.

Simple answer. Use Long instead of Integer. Basically, the Integer type is too small to hold the values you are using in your sample.
The output of your test code is NDIzNzA4OTQ3Mw== - coded through VS Express, just to show that this actually works.

Related

implicit conversion from double to string vb.net

I try to run this code but iget the message implicit conversion from double to string vb.net i try different conversion but its the same
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = TotalTTc) - val(TxtRemise.Text)
End Sub
You perform a mathematical operation, which will obviously result in a number, and you then assign that to the Text property of a TextBox, which is type String. If you want to assign to a String property then you need to assign a String. If you have a number, that means calling ToString on it.
I checked your code. There was some little mistakes. Check my example and comment if you had the solution to your question, thank you.
Public Class Form1
Private Sub TxtRemise_TextChanged(sender As Object, e As EventArgs) Handles TxtRemise.TextChanged
Dim TotalTTc As Decimal
TotalTTc = CDec(TotalHT.Text) + CDec(TXTMontantTva.Text)
TxtTTC.Clear()
TxtTTC.Text = (TotalTTc - CDec(TxtRemise.Text)).ToString
End Sub
End Class

Ternary operation in VB.Net

I am using VB.Net with Visual Studio 2019
Public Shared intDbEnvironment As Integer = CInt(Command().Split(" ")(0))
Sometimes if no parameter is passed, the app crashes. Is there a ternary operator or something that I could use that sets the value to anything if its Nothing or "" or not a number
I've tried
Public Shared intDbEnvironment As Integer = Int32.Parse((Command().Split(" ")(0)))
TryParse Code
Dim success As Boolean = Int32.TryParse((Command().Split(" ")(0)), intDbEnvironment)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i As Integer
Integer.TryParse(TextBox1.Text, i)
Debug.Print(i.ToString)
End Sub
If I enter a non integer in the text box - Guess what! I get 0. Because the Function will not attempt to add a value to i if it is not an integer. The default value of and Integer is 0 so that is the value from the declare.

Assigning value of User Defined Class (UDC) to textbox

Im learning about UDC in vb.net and trying to get a very simple program to display the value of a UDC in a textbox my code follows below:
Structure carDriverInfo
Dim carMake As String
Dim driverName As String
End Structure
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim car As carDriverInfo
Dim driver As carDriverInfo
car.carMake = "Ford Fiesta"
driver.driverName = "J Hudgsons"
TextBox1.Text = car
TextBox2.Text = driver
End Sub
The problem is the compiler gives me the error that CarDriverInfo cannot get converted to string...
What am I doing wrong here?
Compiler doesn't know what variable to show. You can choose one value that will represent string output of your structure by adding following to your structure
Public Overrides Function ToString() As String
Return carName
End Function
But if you want to get different values from your structure you need to define your variables as properties and use those properties as you see fit like
car.carName

Search from a list of string() in VB.NET

I am trying to find if mylines() contains a value or not. I can get the value by mylines.Contains method:
Dim mylines() As String = IO.File.ReadAllLines(mypath)
If mylines.Contains("food") Then
MsgBox("Value Exist")
End If
But the problem is that I want to check if mylines() contains a line which starts with my value. I can get this value from a single line by mylines(0).StartsWith method. But how do I find a string from all the lines which is starting from some value, e.g. "mysearch" and then get that line number?
I am using a for loop to do so, but it is slow.
For Each line In mylines
If line.StartsWith("food") Then MsgBox(line)
Next
Constrained to code for .NET 2.0, please.
Here's one way with Framework 2.0 code, simply set SearchString with the the string you want to search for:
Imports System.IO
Public Class Form1
Dim SearchString As String = ""
Dim Test() As String = File.ReadAllLines("Test.txt")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SearchString = "Food"
End Sub
Private Function StartsWith(s As String) As Boolean
Return s.StartsWith(SearchString)
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim SubTest() As String = Array.FindAll(Test, AddressOf StartsWith)
ListBox1.Items.AddRange(SubTest)
End Sub
End Class
When I test this with a file with 87,000 lines, it takes about .5 sec, to fill the listbox.
I'm not a VB guy, but I think you could use Linq:
Dim mylines() As String = IO.File.ReadAllLines(mypath)
??? = mylines.Where(Function(s) s.StartWith("food")) //not sure of the return type
Check out: How do I append a 'where' clause using VB.NET and LINQ?

Splitting a 'Decimal' in VB.NET

I have 532.016, and I want to get only the 532 part in VB.NET. How can I do that?
Math.Truncate(myDecimal)
will strip away the fractional part, leaving only the integral part (while not altering the type; i. e. this will return the type of the argument as well, be it Double or Decimal).
Cast it to an Integer.
Dim myDec As Decimal
myDecimal = 532.016
Dim i As Integer = Cint(myDecimal)
'i now contains 532
You could use System.Text.RegularExpressions:
Imports System.Text.RegularExpressions 'You need this for "Split"'
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourNumber As String = 532.016 'You could write your integer as a string or convert it to a string'
Dim whatYouWant() As String 'Notice the "(" and ")", these are like an array'
whatYouWant = Split(yourNumber, ".") 'Split by decimal'
MsgBox(whatYouWant(0)) 'The number you wanted is before the first decimal, so you want array "(0)", if wanted the number after the decimal the you would write "(1)"'
End Sub
End Class
Decimal.floor(532.016) will also return 532.
Decimal.Floor rounds down to the closest integer.
However it won't work for negative numbers. See this Stack Overflow question for a complete explanation
Decimal.Truncate (or Math.Truncate) is really the best choice in your case.