Using VB trying to calculate the checksum after input in binary - vb6

I can't figure out how to calculate the checksum after input in text1 for the data, input text1 for the divisor in binary bytes. I tried checksum.text = Text1.Text Xor Text2.Text but its not working, i searched already in internet but its only apply for C++ and java, is it possible in VB?

You cannot XOR on a string. You must do it on 2 numbers, not a string.
Try:
checksum.text = CStr(Clng(Text1.Text) Xor CLng(Text2.Text))

#George
Private Sub Command1_Click()
If Len(Text1.Text) & (Text2.Text) = 0 Or Text1.Text & Text2.Text) Like "[!0-1]" Then
Text3 = "Wrong Input, Please Correct it!!"
Else
checksum.Text = CStr(CLng(Text1.Text) Xor CLng(Text2.Text))
Trans(2).Text = (Text1.Text) + (checksum.Text)
Text3 = "Congratulation CRC is generated"
End If
End Sub

Related

CRC calculation wrong output

I am trying to figure out a CRC check for a serial controlled device.
I have an example, when I send this example to the device, it responds correctly.
This is the complete serial string which the device responds to:
\x00\x17\x3d\x30\x32\x32\x30\x39\x39\x30\x30\x30\x30\x30\x30\x30\x30\x30\x37\x34\x30\x30\x30\x30\x30\x01\x28
(The last 2 bytes (\x01\x28) are the CRC outcomes).
This is my code:
Dim Send As String
Dim CRC1 As String
Dim CRC2 As String
Dim TEMP As String
Private Sub Command1_Click()
Send = &H0 & &H17 & "=" & "022" & "099" & "00" & "00" & "0000074" & "00000"
CRC1 = &H0
CRC2 = &H0
TEMP = &H0
For i = 1 To Len(Send)
TEMP = CRC1
CRC1 = CRC2 Xor Asc(Mid$(Send, i, 1))
CRC2 = TEMP
Next i
Text1.Text = "CRC1= " & CRC1 & " / CRC2= " & CRC2
End Sub
The output should be: CRC1 = 1 (decimal) and CRC2 = 40 (decimal)
But I am getting 51/60.
I think is has something to do with datatypes.
This is the original CRC formula from the device:
Set <CRC1> and <CRC2> to zero.
For every <CHAR> in <MSG> do
<TEMP> = <CRC1>
<CRC1> = <CRC2> XOR <CHAR>
<CRC2> = <TEMP>
Thanks in advance!
You should have looked at your string Send to make sure that it's what you wanted. It isn't. Its construction needs to be:
Send = Chr(&H0) & Chr(&H17) & ...
Then you get the answer you're looking for.
By the way, the thing you are computing is not in any way a CRC. Whoever wrote that formula had no idea what they were talking about. What they constructed is a lousy check value algorithm, so they also had no idea what they were doing.

How can I invert the case of a string in VB6?

I'm trying to make a program that can take the letters of a string and invert their case. I know that in vb.net there exists the IsUpper() command, but I don't know of such a thing in vb6.
What can I use in place of it?
Thanks!
Something like this should work:
Private Function Invert(strIn As String) As String
Dim strOut As String
Dim strChar As String
Dim intLoop As Integer
For intLoop = 1 To Len(strIn)
strChar = Mid(strIn, intLoop, 1)
If UCase(strChar) = strChar Then
strChar = LCase(strChar)
Else
strChar = UCase(strChar)
End If
strOut = strOut + strChar
Next
Invert = strOut
End Function
This loops through the supplied string, and extracts each character. It then tries to convert it to upper case and checks it against the extracted character. If it's the same then it was already upper case, so it converts it to lower case.
It handles non alpha characters just fine as UCase/LCase ignores those.

i want to know the times of occurrance one word in the text-box in VB 0.6

want to know the count of occurrence one word in the text box in Visual BASIC v.0.6 ?
i tried to use the counter from the list but it was not good
for example: in the following sentence:
" go to play and go to home"... the verb "go" appear 2 times ..then.. i want the code that count the number of occurrence of verb "go" and say to me through label for example: 2 times
for example: in the following sentence:
" go to play and go to home"... the verb "go" appear 2 times ..then.. i want the code that count the number of occurrence of verb "go" and say to me through label for example: 2 times
You can use replace() to remove the word from the string, and then compare the length resulting string with the length of the original string, and devide that by the length of the word
Option Explicit
Private Sub Command1_Click()
Label1.Caption = CStr(CountWord(Text1.Text, "go")) & " times"
End Sub
Private Sub Form_Load()
Text1.Text = " go to play and go to home"
End Sub
Private Function CountWord(strText As String, strWord As String) As Long
CountWord = (Len(strText) - Len(Replace(strText, strWord, ""))) / Len(strWord)
End Function
the function CountWord above finds the amount of strWord in strText, it doesn't search for separate words, but also adds one to the count if strWord is part of a larger word
For example " go to play and go to home to search on google" would count 3 instances of "go"
Try the following code: -
Dim txt$, find$, i1%, count%
txt = "go to play and go to home"
find = "go"
i1 = 0
Do
i1 = InStr(i1 + 1, txt, find)
If i1 > 0 Then
count = count + 1
i1 = i1 + Len(find)
Else
Exit Do
End If
Loop
MsgBox count
You can use regular expressions to count word occurrences
Private Sub Form_Load()
Debug.Print CountWords(" go to play and G.O to home to search on g.o" & vbCrLf & "ogle", "g.o")
End Sub
Private Function CountWords(sText As String, sWord As String) As Long
Dim sPattern As String
sPattern = pvInitRegExp("[-[\]{}()*+?.,\\^$|#\s]").Replace(sWord, "\$&")
CountWords = pvInitRegExp("\s" & sPattern & "\s").Execute(sText).Count
End Function
Private Function pvInitRegExp(sPattern As String) As Object
Set pvInitRegExp = CreateObject("VBScript.RegExp")
With pvInitRegExp
.Pattern = sPattern
.Global = True
.IgnoreCase = True
.MultiLine = True
End With
End Function
This takes care of word boundaries too so "google" is not counted.

VB Validate TextBox input to be Binary Number

I am trying to validate the input of a TextBox to make sure it is a Binary Number. What I have so far is:
Private Sub Command1_Click()
Dim b() As Byte
b = Text1.Text
If Not IsByte (b) Then
Text3 = "Wrong input"
Else
Text3 = "CRC is generated"
' checksum.Text = Text1.Text Xor Text2.Text
' Trans(2).Text = (Text1.Text) + (checksum.Text)
End If
Input in Text1 should only be accepting binary numbers, so only 1 or 0 should be allowed.
You can use Like here:
Private Sub Command1_Click()
If Len(Text1.Text) = 0 Or Text1.Text Like "*[!0-1]*" Then
MsgBox "bad binary string"
Else
MsgBox "good binary string"
End If
End Sub
This pattern is testing for "0 to many of anything, followed by one character not in the range 0 through 1, then 0 to many of anything."
Below is the code. Request you to kindly google before posting any questions to this forum.
'will only allow 0 and 1
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey1
Case Else
KeyAscii = 0
End Select
End Sub
' will validate if its numeric and you can further check for 0 or 1
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "Please enter numbers only.", vbInformation
'you may also consider erasing it
Text1.Text = ""
End If
End Sub
Even i cant find the function to check binary data. But cant you just check like
If textbox1.text = 1 or textbox1.text = 2
I think you can also do with instr function.

How to reduce the decimal length

I want to reduce the decimal length
text1.text = 2137.2198231578
From the above, i want to show only first 2 digit decimal number
Expected Output
text1.text = 2137.21
How to do this.
Format("2137.2198231578", "####.##")
I was about to post use Format() when I noticed p0rter comment.
Format(text1.text, "000.00")
I guess Int() will round down for you.
Been many years since I used VB6...
This function should do what you want (inline comments should explain what is happening):
Private Function FormatDecimals(ByVal Number As Double, ByVal DecimalPlaces As Integer) As String
Dim NumberString As String
Dim DecimalLocation As Integer
Dim i As Integer
Dim LeftHandSide As String
Dim RightHandSide As String
'convert the number to a string
NumberString = CStr(Number)
'find the decimal point
DecimalLocation = InStr(1, NumberString, ".")
'check to see if the decimal point was found
If DecimalLocation = 0 Then
'return the number if no decimal places required
If DecimalPlaces = 0 Then
FormatDecimals = NumberString
Exit Function
End If
'not a floating point number so add on the required number of zeros
NumberString = NumberString & "."
For i = 0 To DecimalPlaces
NumberString = NumberString & "0"
Next
FormatDecimals = NumberString
Exit Function
Else
'decimal point found
'split out the string based on the location of the decimal point
LeftHandSide = Mid(NumberString, 1, DecimalLocation - 1)
RightHandSide = Mid(NumberString, DecimalLocation + 1)
'if we don't want any decimal places just return the left hand side
If DecimalPlaces = 0 Then
FormatDecimals = LeftHandSide
Exit Function
End If
'make sure the right hand side if the required length
Do Until Len(RightHandSide) >= DecimalPlaces
RightHandSide = RightHandSide & "0"
Loop
'strip off any extra didgits that we dont want
RightHandSide = Left(RightHandSide, DecimalPlaces)
'return the new value
FormatDecimals = LeftHandSide & "." & RightHandSide
Exit Function
End If
End Function
Usage:
Debug.Print FormatDecimals(2137.2198231578, 2) 'outputs 2137.21
Looks fairly simple, but I must be missing something subtle here. What about:
Option Explicit
Private Function Fmt2Places(ByVal Value As Double) As String
Fmt2Places = Format$(Fix(Value * 100#) / 100#, "0.00")
End Function
Private Sub Form_Load()
Text1.Text = Fmt2Places(2137.2198231578)
End Sub
This also works in locales where the decimal point character is a comma.

Resources