Help with partial strig operations???
Author |
Message |
Mark B #1 / 14
|
Help with partial strig operations???
Hello all, I've got a problem, I have a string 'Hello World!', and I have got it to its ASCII form, separating each character code by a space, ie: '72 101 108 108 111 32 87 111 114 108 100 33 '. I need to return each 'bundle' of numbers, ie: between the spaces, and perform some operations on them separately as I go and put the finished result into a field when done. Here's my code: dim StrEnd As integer Dim i As integer dim int As integer dim int2 As integer dim s As string dim mystr As string StrEnd = len(EditField1.Text) mystr=editfield1.text int2=1 for i=0 to StrEnd int=instr(int2,mystr,chr(32)) s=s+mid(mystr,int2,(int-int2)) int2=int //Here's where I'll perform my ops on the characters editfield2.text=editfield2.text+s next I'm probably way off here, but any help would be greately appreciated. At the moment, it returns '72727272727272727272727272727272727272727272727272727272727272727272727 2727272727272727272' Thank you again, in advance, Mark Bee.
|
Tue, 14 Sep 2004 13:51:43 GMT |
|
|
Roel Griffio #2 / 14
|
Help with partial strig operations???
Quote:
> int2=int
Maybe int2=int+1 -- Groeten, Roel Griffioen
|
Tue, 14 Sep 2004 18:03:54 GMT |
|
|
Jacques Thériaul #3 / 14
|
Help with partial strig operations???
Quote: > dim StrEnd As integer > Dim i As integer > dim int As integer > dim int2 As integer > dim s As string > dim mystr As string > StrEnd = len(EditField1.Text) > mystr=editfield1.text > int2=1 > for i=0 to StrEnd > int=instr(int2,mystr,chr(32)) > s=s+mid(mystr,int2,(int-int2)) > int2=int > //Here's where I'll perform my ops on the characters > editfield2.text=editfield2.text+s > next > I'm probably way off here, but any help would be greately appreciated. > At the moment, it returns > '72727272727272727272727272727272727272727272727272727272727272727272727 > 2727272727272727272'
try something like this dim s, gotit as string dim i as integer s = "72 101 108 108 111 32 87 111 114 108 100 33 " i = 1 gotit = nthfield(editfield1.text," ",i) do 'perform what you want here editfield2.seltext = gotit + chr(13) i = i + 1 gotit = nthfield(editfield1.text," ",i) loop until gotit = "" 'DONE This is much nicer and you don't have to know in advance how many items you have. But if you were trying to remove blank space then try this dim s as string s=ReplaceAll(Editfield1.text," ","") editfield2.text = s Have a nice weekend! Jacques
|
Tue, 14 Sep 2004 19:18:50 GMT |
|
|
Chris Willi #4 / 14
|
Help with partial strig operations???
Try this: --------- dim i, j, count, count1 as integer dim result, totalresult, thetext as string count1 = countfields(EditField1.text, " ") for i = 1 to count1 result = "" thetext = nthfield(EditField1.text, " ", i) count = len(thetext) for j = 1 to count If result <> "" then result = result + " " + str(asc(mid(thetext, j, 1))) else result = str(asc(mid(thetext, j, 1))) end if next totalresult = totalresult + result // do your op on the bundle here next EditField2.text = totalresult --------- Regards. -------- Chris Willis
|
Tue, 14 Sep 2004 21:56:45 GMT |
|
|
Jacques Thériaul #5 / 14
|
Help with partial strig operations???
Quote:
> Try this: > --------- > dim i, j, count, count1 as integer > dim result, totalresult, thetext as string > count1 = countfields(EditField1.text, " ") > for i = 1 to count1 > result = "" > thetext = nthfield(EditField1.text, " ", i) > count = len(thetext) > for j = 1 to count > If result <> "" then > result = result + " " + str(asc(mid(thetext, j, 1))) > else > result = str(asc(mid(thetext, j, 1))) > end if > next > totalresult = totalresult + result // do your op on the bundle here > next > EditField2.text = totalresult
I think you missed the point here. Mark Bee wanted to act on individual chumk, you are returning "55 5049 48 4949 48 5649 48 5649 49 4951 5056 5549 49 4949 49 5249 48 5649 48 4851 51" Jacques
|
Tue, 14 Sep 2004 22:15:24 GMT |
|
|
Jacques Thériaul #6 / 14
|
Help with partial strig operations???
Quote:
>> I'm probably way off here, but any help would be greately appreciated. >> At the moment, it returns >> '72727272727272727272727272727272727272727272727272727272727272727272727 >> 2727272727272727272' > try something like this > dim s, gotit as string > dim i as integer > s = "72 101 108 108 111 32 87 111 114 108 100 33 " > i = 1 > gotit = nthfield(editfield1.text," ",i) > do > 'perform what you want here > editfield2.seltext = gotit + chr(13) > i = i + 1 > gotit = nthfield(editfield1.text," ",i) > loop until gotit = "" > 'DONE
Using 'NthField' is probably OK for not too long string, but what happend if the string is in the thousands or hundred of thousands or even millions characters. The 'NthField' always start its scans from the beginning, so if you have a very long string then it becomes inefficient. A better aproach for very long string is to do the scan manually and in one sweep. Like this: dim l,n as integer dim s,t as string l = len(editfield1.text) n = 1 editfield2.text = "" while n < l s = "" t = mid(editfield1.text,n,1) while t <> " " and n <= l s = s + t n = n + 1 t = mid(editfield1.text,n,1) wend editfield2.seltext = s + chr(13) n = n + 1 t = mid(editfield1.text,n,1) while t = " " and n <= l n = n + 1 t = mid(editfield1.text,n,1) wend wend This routine will make only one pass eliminating any duplicate 'spaces' found on its way. Jacques Thriault
|
Tue, 14 Sep 2004 22:23:23 GMT |
|
|
Chris Willi #7 / 14
|
Help with partial strig operations???
Quote:
>> Try this: >> --------- >> dim i, j, count, count1 as integer >> dim result, totalresult, thetext as string >> count1 = countfields(EditField1.text, " ") >> for i = 1 to count1 >> result = "" >> thetext = nthfield(EditField1.text, " ", i) >> count = len(thetext) >> for j = 1 to count >> If result <> "" then >> result = result + " " + str(asc(mid(thetext, j, 1))) >> else >> result = str(asc(mid(thetext, j, 1))) >> end if >> next >> totalresult = totalresult + result // do your op on the bundle here >> next >> EditField2.text = totalresult > I think you missed the point here. > Mark Bee wanted to act on individual chumk, you are returning > "55 5049 48 4949 48 5649 48 5649 49 4951 5056 5549 49 4949 49 5249 48 > 5649 48 4851 51" > Jacques
Quote: "I need to return each 'bundle' of numbers, ie: between the spaces, and perform some operations on them separately as I go and put the finished result into a field when done." I'm sure Mark is being very clear on this, does he want to do operations on each character or on each word? Regards. -------- Chris Willis
|
Tue, 14 Sep 2004 23:42:15 GMT |
|
|
Jacques Thériaul #8 / 14
|
Help with partial strig operations???
Quote:
>>I think you missed the point here. >>Mark Bee wanted to act on individual chumk, you are returning >>"55 5049 48 4949 48 5649 48 5649 49 4951 5056 5549 49 4949 49 5249 48 >>5649 48 4851 51" >>Jacques > Quote: "I need to return each 'bundle' of numbers, ie: between the spaces, > and perform some operations on them separately as I go and put the finished > result into a field when done." > I'm sure Mark is being very clear on this, does he want to do operations on > each character or on each word?
Ok so lets take his string: '72 101 108 108 111 32 87 111 114 108 100 33 ' each bundle of numbers to me seems to be (72) then (101) then (108) and so on. He wants to act on each character that were previously converted to their ascii codes, and that shows in the string that he posted. He had difficulyt getting each group of numbers individally ie: 72,101,108 etc. Jacques Thriault
|
Tue, 14 Sep 2004 23:59:27 GMT |
|
|
Theo #9 / 14
|
Help with partial strig operations???
Quote:
>Using 'NthField' is probably OK for not too long string, but what >happend if the string is in the thousands or hundred of thousands or >even millions characters.
Try FastNthField on my website. www.elfdata.com/programmer/ -- Email Cleaner does convenient re-wrapping on badly wrapped emails and newsgroup-posts. Badly wrapped lines is a problem every Internet User comes across, Email Cleaner solves it! It also has many other features. Email Cleaner works from inside all of your Mac programs, from one button click. <www.elfdata.com/emailcleaner/>
|
Wed, 15 Sep 2004 01:24:03 GMT |
|
|
Joe Strou #10 / 14
|
Help with partial strig operations???
Quote:
> >Using 'NthField' is probably OK for not too long string, but what > >happend if the string is in the thousands or hundred of thousands or > >even millions characters. > Try FastNthField on my website. www.elfdata.com/programmer/
Or, just loop yourself using InStr and Mid. (Or if you're sure you've only got 7-bit ASCII characters, InStrB and MidB.) Cheers, - Joe -- ,------------------------------------------------------------------. | Joseph J. Strout Check out the Mac Web Directory: |
`------------------------------------------------------------------'
|
Wed, 15 Sep 2004 02:13:35 GMT |
|
|
matt neubu #11 / 14
|
Help with partial strig operations???
Quote:
> Hello all, > I've got a problem, I have a string 'Hello World!', and I have got it to > its ASCII form, separating each character code by a space, > ie: '72 101 108 108 111 32 87 111 114 108 100 33 '.
This is almost exactly what's done on p. 191 of my book; shouldn't take you long to adapt that code to your purposes (mostly, just throw away the hex conversion). m. --
*** REALbasic: The Definitive Guide! 2nd edition! *** http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt
|
Wed, 15 Sep 2004 02:26:14 GMT |
|
|
Mark B #12 / 14
|
Help with partial strig operations???
Quote:
>Like this: >[SNIP] > Jacques Thriault
Thanks, it seems to do the job I want exactly, and the best way, I think. I have adapted it to my requirements, but I am having a problem. I'll show you the code, although it's a bit lengthy at the moment: Dim l,int As Integer Dim s,t As String //For encrypting purposes Dim Cipher As String Dim TheString As String Dim MyString As String Dim TheNewString As String Dim temp1 As String Dim temp2 As String Dim NUndo As String Dim N As String Dim result As String Dim length As Integer MyString = DoASCII(MessageField.Text) //Set the MP Plugin... MPLSetPrecision 1000 //Calculate N... N = MPLMul(pField.Text,qField.Text) //Claculate NUndo... temp1 = MPLSub(pField.Text,"1") temp2 = MPLSub(qField.Text,"1") NUndo = MPLMul(temp1,temp2) //Show the red field's values... NField.Text=N PQField.Text=NUndo l = Len(MyString) int = 1 CipherTextField.Text = "" While int < l s = "" t = Mid(MyString,int,1) While t <> " " and int <= l s = s + t int = int + 1 t = Mid(MyString,int,1) Wend Cipher = "" MsgBox s //***************************************** //Birng s up to the power e... s = MPLPow(MyString,eField.Text) //Calculate ciphertext from TheNewString and N... Cipher = MPLMod(s,N) CipherTextField.SelText = Cipher + Chr(32) int = int + 1 t = Mid(MyString,int,1) While t = " " and int <= l int = int + 1 t = Mid(MyString,int,1) Wend Wend The problem I am having is: Where the comment of stars are, I have used Robert Delaney's Precision Plugin to perform some operations on the ASCII characters for my encryption program, but there is a problem somewhere near the line of stars. I keep getting the same result repetitively, depending on what the first character of the 'to-be-encypted' message is. I stuck the line 'MsgBox s' into the code, (just above the stars), to see what 's' is, everytime the loop reaches that part of the code. 's' seems to be updating, (corresponding to the ASCII code of the next letter in the message), but when I perform the MPLPow() and MPLMod() ops. on it, it doesn't updte properly to the next letter - it stays the same, and hence I keep getting the same number returned in every iteration. Thanks if you an help, I hope you understand what I'm trying to say! :-) I've spent hours looking at the code trying to figure out where the error might be - I bet it's one line somewhere that needs to be tweaked... (I should call this code Open Source, now!) Mark Bee.
|
Wed, 15 Sep 2004 18:40:19 GMT |
|
|
Jacques Thériaul #13 / 14
|
Help with partial strig operations???
Quote:
>>Like this: >>[SNIP] >>Jacques Thriault > Thanks, it seems to do the job I want exactly, and the best way, I > think. I have adapted it to my requirements, but I am having a problem. > I'll show you the code, although it's a bit lengthy at the moment: > Dim l,int, As Integer > Dim s,t As String > //For encrypting purposes > Dim Cipher As String > Dim TheString As String > Dim MyString As String > Dim TheNewString As String > Dim temp1 As String > Dim temp2 As String > Dim NUndo As String > Dim N As String > Dim result As String > Dim length As Integer > MyString = DoASCII(MessageField.Text) > //Set the MP Plugin... > MPLSetPrecision 1000 > //Calculate N... > N = MPLMul(pField.Text,qField.Text) > //Claculate NUndo... > temp1 = MPLSub(pField.Text,"1") > temp2 = MPLSub(qField.Text,"1") > NUndo = MPLMul(temp1,temp2) > //Show the red field's values... > NField.Text=N > PQField.Text=NUndo > l = Len(MyString) > int = 1 > CipherTextField.Text = "" > While int < l > s = "" > t = Mid(MyString,int,1) > While t <> " " and int <= l > s = s + t > int = int + 1 > t = Mid(MyString,int,1) > Wend > Cipher = "" > MsgBox s > //***************************************** > //Birng s up to the power e... > s = MPLPow(MyString,eField.Text)
You are not doing s^e here but MyString^e which is quite different. Moreover you are doing that repeatedly every loop with the same myString....no wonder you are getting the same result every loop. Try: s = MPLPow(s,eField.Text) Reusing variable names within a function is the "Tar Pit" of programming. Quote: > //Calculate ciphertext from TheNewString and N... > Cipher = MPLMod(s,N) > CipherTextField.SelText = Cipher + Chr(32) > int = int + 1 > t = Mid(MyString,int,1) > While t = " " and int <= l > int = int + 1 > t = Mid(MyString,int,1) > Wend > Wend > I've spent hours looking at the code trying to figure out where the > error might be - I bet it's one line somewhere that needs to be > tweaked... (I should call this code Open Source, now!) > Mark Bee.
Running code with a 'carbon CPU' versus 'Sillicon CPU' produce quite different things. As you'll do this more often, your 'carbon CPU' (ie:brain) will get better and better at it. This is why having its code checked by another person is so important, even for experienced programmer. Programming should be an ego-less activity. Even the best programmer do stupid mistakes, and doing mistakes only proves that we are human. Keep trying Jacques
|
Wed, 15 Sep 2004 22:40:26 GMT |
|
|
Mark B #14 / 14
|
Help with partial strig operations???
Quote: > You are not doing s^e here but MyString^e which is quite different. > Moreover you are doing that repeatedly every loop with the same > myString....no wonder you are getting the same result every loop. > Try: s = MPLPow(s,eField.Text) > Reusing variable names within a function is the "Tar Pit" of programming.
Thanks Jacques. It works just as I wanted now. I thought it was something small! Anyway, Thanks again, Mark Bee.
|
Thu, 16 Sep 2004 16:12:53 GMT |
|
|
|