| Mirage Source http://www.miragesource.net/forums/ |
|
| Replace$ error http://www.miragesource.net/forums/viewtopic.php?f=201&t=5123 |
Page 1 of 2 |
| Author: | Pbcrazy [ Tue Feb 24, 2009 10:35 pm ] |
| Post subject: | Replace$ error |
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 |
|
| Author: | Asrrin29 [ Wed Feb 25, 2009 12:31 am ] |
| Post subject: | Re: Replace$ error |
If you have a character whose ASCII code is close to the end, adding numbers to the Chr function might cause it to error. try throwing a check in their to reset Chr() from 0 if it reaches the end of the code list. |
|
| Author: | Pbcrazy [ Wed Feb 25, 2009 12:58 am ] | ||
| Post subject: | Re: Replace$ error | ||
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 <= 255 Then Message = Replace$(Message, Chr(i), Chr(i + 2)) Else Message = Replace$(Message, Chr(i), Chr(0 + ((255 - i) + 2))) End If Next i txtOutput.Text = Message End Sub
|
|||
| Author: | Dragoons Master [ Wed Feb 25, 2009 1:33 am ] |
| Post subject: | Re: Replace$ error |
Dont use an if. Use Mod operator: Code: If i + checknum <= 255 Then Message = Replace$(Message, Chr(i), Chr(i + 2)) Else Message = Replace$(Message, Chr(i), Chr(0 + ((255 - i) + 2))) End If becomes Code: Message = Replace$(Message, Chr(i), Chr((i + 2) Mod 256)) (Mod 256) range is 0 - 255 |
|
| Author: | Pbcrazy [ Wed Feb 25, 2009 1:36 am ] |
| Post subject: | Re: Replace$ error |
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 |
|
| Author: | Egon [ Wed Feb 25, 2009 2:40 am ] |
| Post subject: | Re: Replace$ error |
Right before a "message =" put msgbox chr(i) just to see what character it's on. |
|
| Author: | Pbcrazy [ Wed Feb 25, 2009 1:05 pm ] |
| Post subject: | Re: Replace$ error |
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 <= 126 Then Message = Replace$(Message, Chr(i), Chr(i + 2)) 'Else 'Message = Replace$(Message, Chr(i), Chr(32 + ((126 - i) + 2))) 'End If Next i txtOutput.Text = Message End Sub |
|
| Author: | Egon [ Wed Feb 25, 2009 8:34 pm ] |
| Post subject: | Re: Replace$ error |
What are EncryptedMsg and checknum doing in that sub anyway? |
|
| Author: | Pbcrazy [ Wed Feb 25, 2009 10:15 pm ] |
| Post subject: | Re: Replace$ error |
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. |
|
| Author: | Egon [ Wed Feb 25, 2009 11:03 pm ] |
| Post subject: | Re: Replace$ error |
Change Code: For i = 32 To 126 to Code: For i = 65 To 90 You should understand that to do from there. |
|
| Author: | Dragoons Master [ Thu Feb 26, 2009 12:47 pm ] |
| Post subject: | Re: Replace$ error |
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. |
|
| Author: | Pbcrazy [ Thu Feb 26, 2009 1:03 pm ] |
| Post subject: | Re: Replace$ error |
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 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 <= 255 Then Message = Replace$(Message, Chr(i), Chr(i + 2) Mod (255)) 'Else 'EncryptedMsg = Replace$(Message, Chr(i), Chr(0 + ((255 - i) + 2))) 'End If Next i txtOutput.Text = Message End Sub at the like with Mod in it, im not sure where the mismatch is, and right now unfortunately i don't have time to look for it :S |
|
| Author: | Dragoons Master [ Fri Feb 27, 2009 12:50 am ] |
| Post subject: | Re: Replace$ error |
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 |
|
| Author: | Pbcrazy [ Fri Feb 27, 2009 1:14 am ] |
| Post subject: | Re: Replace$ error |
heh, nice i didn't think of that lol, thanks a bunch! |
|
| Author: | Pbcrazy [ Fri Feb 27, 2009 1:35 am ] | ||
| Post subject: | Re: Replace$ error | ||
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
|
|||
| Page 1 of 2 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|