Mirage Source
Replace$ error - Printable Version

+- Mirage Source (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: General (https://mirage-engine.uk/forums/forumdisplay.php?fid=17)
+---- Thread: Replace$ error (/showthread.php?tid=2571)



Replace$ error - Pbcrazy - 24-02-2009

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


Re: Replace$ error - Pbcrazy - 25-02-2009

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


Re: Replace$ error - Dragoons Master - 25-02-2009

Dont use an if. Use Mod operator:
[code] If i + checknum


Re: Replace$ error - Pbcrazy - 25-02-2009

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


Re: Replace$ error - Egon - 25-02-2009

Right before a "message =" put msgbox chr(i) just to see what character it's on.


Re: Replace$ error - Pbcrazy - 25-02-2009

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


Re: Replace$ error - Egon - 25-02-2009

What are EncryptedMsg and checknum doing in that sub anyway?


Re: Replace$ error - Pbcrazy - 25-02-2009

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.


Re: Replace$ error - Egon - 25-02-2009

Change
Code:
For i = 32 To 126
to
Code:
For i = 65 To 90
You should understand that to do from there.


Re: Replace$ error - Dragoons Master - 26-02-2009

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.


Re: Replace$ error - Pbcrazy - 26-02-2009

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


Re: Replace$ error - Dragoons Master - 27-02-2009

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



Re: Replace$ error - Pbcrazy - 27-02-2009

heh, nice i didn't think of that :oops:

lol, thanks a bunch!


Re: Replace$ error - Pbcrazy - 27-02-2009

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