Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace$ error
#1
well, obviously i've been away from MS/VB6 for a while, and i wanted to make a quick simple encryption program in VB, which just advanced the ASCII characters by a certain amount. However i have gotten an error.

Code:
Private Sub cmdEncrypt_Click()
Message = txtInput.Text
Dim EncryptedMsg As String
Dim checknum As Long
checknum = Rand(1, 9)
For i = 0 To 255
    EncryptedMsg = Replace$(Message, Chr(i), Chr(i + checknum))
Next i

txtOutput.Text = EncryptedMsg

End Sub

Function Rand(ByVal Low As Integer, ByVal High As Integer) As Integer
    Randomize
    Rand = ((High - Low + 1) * Rnd) + Low
End Function

i get a run-time error 5: invalid procedure call or argument, and highlights this line

Code:
EncryptedMsg = Replace$(Message, Chr(i), Chr(i + checknum))

thanks,
-Pb
Reply
#2
hmph, well i kinda got it to work, it doesn't give me an error anymore, however the displayed text is...not text?

[code]Private Sub cmdEncrypt_Click()
Message = txtInput.Text
Dim EncryptedMsg As String
Dim checknum As Long
checknum = Rand(1, 9)
For i = 0 To 255
If i + checknum
Reply
#3
Dont use an if. Use Mod operator:
[code] If i + checknum
Reply
#4
hmm, i've never seen that before...

tried it, still didn't work, actually kinda made it worse, now i just get one NULL character instead of at least the same amount. :S
Reply
#5
Right before a "message =" put msgbox chr(i) just to see what character it's on.
Reply
#6
hmph, i ran that, and remembered that several of the beginning "characters" are NULL, so i changed the for loop variables, and tried it, all i got was "#"#"# repeating for the amount of characters i put in.

so i took out the if, then all i got was €€€€€€€€€€€€€

note: i had put in "abcdefghijklmnopqrstuvwxyz"
[code]Private Sub cmdEncrypt_Click()
Message = txtInput.Text
Dim EncryptedMsg As String
Dim checknum As Long
checknum = Rand(1, 9)
For i = 32 To 126
'If i + 2
Reply
#7
What are EncryptedMsg and checknum doing in that sub anyway?
Reply
#8
well they had been in there prior for part of the encrypting, however i was doing some debugging and took them out for the time being.
Reply
#9
Change
Code:
For i = 32 To 126
to
Code:
For i = 65 To 90
You should understand that to do from there.
Reply
#10
Actually, make it
Code:
For i = 0 To 255
And use mod operator, it's just easier.

@Egon:
If you use anything else than the above, you will be subject to errors when decrypting.
Reply
#11
alright, and i just kinda realized why i was only getting 2 different characters in the final string, i was adding/replacing in the same string, so the replace command was replacing characters it had just previously replaced. And now i remember why i had the encryptedmsg in there Smile

edit: hmph, alright i tried it, and it came up with a type mismatch error

[code]Private Sub cmdEncrypt_Click()
Message = txtInput.Text
Dim EncryptedMsg As String
Dim checknum As Long
checknum = Rand(1, 9)

For i = 0 To 255
'If i + 2
Reply
#12
Message = Replace$(Message, Chr(i), Chr((i + 2) Mod (255)))
Would be more correct.
And well, you will always get a fucked up string... You can't use the semi-encrypted string to replace, you need a backup, but actually even that will not work. You need to replace each character...
Here is the working function:
Code:
Dim checknum As Long
Dim i As Long
Randomize
checknum = Int((9 * Rnd) + 1)

txtOutput.Text = ""

For i = 1 To Len(txtInput.Text)
    txtOutput.Text = txtOutput.Text & Chr((Asc(Mid(txtInput.Text, i, 1)) + checknum) Mod 256)
Next i
Reply
#13
heh, nice i didn't think of that :oops:

lol, thanks a bunch!
Reply
#14
ok full working program and source for anyway who wants it, albeit it's defenatly not very advanced or anything, it can be kinda fun Smile
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)