String problem
Blitz3D Forums/Blitz3D Beginners Area/String problem
| ||
I've hit a bit of a snag in my program. My string is terminating at the wrong point but I'm not sure why. I'm building my string up from several compressed numbers, using a small compression routine, which I'm certain is fine because I took it from the Blitzplay library. When I follow it through on the debugger, I can see the little strings being built up - for example, they'll show 'A', then 'A^', then 'A^f'. The quotation marks are clear around the string as it builds up. But when I put a certain number into the function, it shows 'Me .... but no closing quotation mark. Then when I add that string to the bigger string, the big string also terminates at that point, and adding anything else to it seems to make no difference (it does seem to get longer, but I can't see anything after that point). The function is as follows. Function IntToStr$(Num%,StrLen%=4) ;-=-=-=Take an Integer and compress it to a string, of "strlen" bytes long. Local shiftin% Local st$=Chr$(num And 255) For shiftin=1 To (strlen-1) st$=st$+Chr$(num Sar(8*shiftin)) Next Return st$ End Function It goes wrong when the number 25933 is put into it. Any ideas? I'm guessing I'm making some really obvious mistake here :( |
| ||
I see no problem with - For NumLoop=25000 To 25001 Compre$=IntToStr(NumLoop,4) Concat$=ConCat$+Compre+";" Next Print ConCat$ Print Len(ConCat) ConCat="" For NumLoop=25933 To 25934 Compre$=IntToStr(NumLoop,4) Concat$=ConCat$+Compre+";" Next Print ConCat$ Print Len(ConCat) WaitKey End Function IntToStr$(Num%,StrLen%=4) ;-=-=-=Take an Integer and compress it to a string, of "strlen" bytes long. Local shiftin% Local st$=Chr$(num And 255) For shiftin=1 To (strlen-1) st$=st$+Chr$(num Sar(8*shiftin)) Next Return st$ End Function |
| ||
Ah, it's a problem with the debugger then. If you follow your program through line-by-line, you'll see that it doesn't update the string properly as it goes along, although it does print them out properly. Strange. It does seem to be causing errors in my server/client though, because it doesn't seem to want to send the string after that point :( |
| ||
Bump. Can someone run the above program and watch it with the debugger? Can you explain why, when it's building up the number 25933 the string suddenly has no terminating "? Is this just a printing error in the debugger, or is something else going on here? It's causing a major headache for me now, because once I add that string to my longer string, anything else I add is ignored by my network library. |
| ||
Converting this linest$=st$+Chr$(num Sar(8*shiftin))into steps shows better what's going on: Stop For NumLoop=25000 To 25001 Compre$=IntToStr(NumLoop,4) Concat$=ConCat$+Compre+";" Next Print ConCat$ Print Len(ConCat) ConCat="" For NumLoop=25933 To 25934 Compre$=IntToStr(NumLoop,4) Concat$=ConCat$+Compre+";" Next Print ConCat$ Print Len(ConCat) WaitKey End Function IntToStr$(Num%,StrLen%=4) ;-=-=-=Take an Integer and compress it to a string, of "strlen" bytes long. Local shiftin% Local st$=Chr$(num And 255) For shiftin=1 To (strlen-1) temp = 8*shiftin temp2 = num Sar(temp) temp3$ = Chr$(temp2) st$ = st$ + temp3$ ;st$=st$+Chr$(num Sar(8*shiftin)) Next Return st$ End Function You see, you are adding a chr$(0), which is NULL. That's why the IDE doesn't display it correctly. Heres one explanation: http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:47238880219973 And an ASCII table: http://www.neurophys.wisc.edu/www/comp/docs/ascii.html |
| ||
Thanks, that explains that then. Leaves me with two options. One is to use Bitstreams for my library, which might be better but will need more converting. Two, is to change the IntToStr (and it's opposite StrToInt) so that it doesn't use anything between 0-31. Does anyone know how I'd do that? |