Combining Strings...
I racked my brain, tried 30 different ways of doing it and still couldn't get it to work. Here's what I'm trying to do in a nutshell:
Here is the data I am working with:
counter VAR WORD ' A number from 0000 to 9999.
fixedword DATA "theword" ' Just a word.
I am trying to created a concatenated string with the end result being:
"0000theword" and each increment of the counter would be "0001theword", "0002theword" and so on.
Ultimately I will be sending this string out a serial port, but for now, I would be happy just to get them combined and output them as a DEBUG command. I'm sure this is an easy thing, but I'm fairly new to this and haven't quite got my syntax down.
Quick word of advice anyone? This is a BS2.
Post Edited (Starion) : 11/27/2005 4:04:20 PM GMT
Here is the data I am working with:
counter VAR WORD ' A number from 0000 to 9999.
fixedword DATA "theword" ' Just a word.
I am trying to created a concatenated string with the end result being:
"0000theword" and each increment of the counter would be "0001theword", "0002theword" and so on.
Ultimately I will be sending this string out a serial port, but for now, I would be happy just to get them combined and output them as a DEBUG command. I'm sure this is an easy thing, but I'm fairly new to this and haven't quite got my syntax down.
Quick word of advice anyone? This is a BS2.
Post Edited (Starion) : 11/27/2005 4:04:20 PM GMT

Comments
' {$STAMP BS2e} ' {$PBASIC 2.5} ' ' Concatenation Example ' str1 VAR Word 'Stores the first string to concatenate str2 VAR Word 'Stores the second string to concatenate concat VAR Word(2) 'Stores the resulting string of the concatenation process '--------------------------------------------- main: str1=65'"a" str2=66'"b" GOSUB concatenate GOSUB debug_output END concatenate: concat(0)=str1 concat(1)=str2 RETURN debug_output: DEBUG HOME, "str1:", str1," str2:", str2," concat:",concat(0),concat(1), CR RETURNPost Edited (Tibot) : 11/27/2005 4:28:13 PM GMT
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR Word eePntr VAR Byte char VAR Byte idx VAR Nib TheWord DATA "0000theword", CR, 0 Reset: counter = 0 Main: DO ' update string ' FOR idx = 0 TO 3 WRITE TheWord + idx, (counter DIG (3-idx)) + "0" NEXT ' print the string ' eePntr = TheWord GOSUB Print_Str ' update counter ' counter = counter + 1 PAUSE 500 LOOP END Print_Str: DO READ eePntr, char eePntr = eePntr + 1 IF (char = 0) THEN EXIT DEBUG char LOOP RETURN▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 11/27/2005 5:01:42 PM GMT
{$STAMP BS2} ' {$PBASIC 2.5} CX VAR Byte 'For next loop counter p VAR Byte 'Bytes for each place, 1,10,100,1000 p1 VAR Byte p2 VAR Byte p3 VAR Byte offset VAR Byte 'The ascii code for "0" N VAR Word ' Your number W VAR Byte(10) ' Your String offset="0" FOR cx=0TO 10 'Read in string to memory variable READ cx,W(cx) 'Locations 0 through 3 placeholder for number default "0000" NEXT n=0 ' Number from 1 to 9999 main: GOSUB stringcon GOSUB display PAUSE 1000 n=n+1 'Your number, for testing I am incrementing IF n>9999 THEN n=0 GOTO main END stringcon: 'extract each number p=n/1000 p1=n -(P*1000) p2=p1 p1=p1/100 p2=p2-(p1*100) p3=p2 p2=p2/10 p3=p3-(p2*10) 'Modify string w(0)=p+offset w(1)=p1+offset w(2)=P2+offset W(3)=P3+offset RETURN display: 'display results DEBUG STR w,CR RETURN DATA "0000theword"▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!
So if you are going to be doing alot of it you may want to convert Jon's program to use RAM memory instead of EEPROM memory. If you leave out the display and pause commands in that one you can burn up your eeprom memory locations 0 through 3 in about an hour. On higher stamps running faster and only 100,000 writes could bee poof before you blink.
I could be wrong about this , if so please correct me....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think outside the BOX!
Here's a "safe" version that is RAM and program space efficient (which is critical in small controllers like BASIC Stamp -- in my opinion using RAM for strings is a huge waste of resources); pass the value you want to print in "temp" and the string you want to append in "eePntr."
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR Word temp VAR Word eePntr VAR Byte char VAR Byte idx VAR Nib TheWord DATA "theword", CR, 0 Reset: counter = 0 Main: DO temp = counter eePntr = TheWord GOSUB Print_Str4 counter = counter + 1 PAUSE 500 LOOP END Print_Str4: DEBUG DEC4 temp DO READ eePntr, char eePntr = eePntr + 1 IF (char = 0) THEN EXIT DEBUG char LOOP RETURN▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 11/27/2005 6:09:26 PM GMT
DO WHILE COUNTER<10000
SEROUT [noparse][[/noparse]DEC4 counter]
SEROUT [noparse][[/noparse]"theword",CR]
counter=counter+1
LOOP
Once I realized that the serial device on the other end doesn't know whether I used one or six SEROUT commands, it all appears continuous until a CR is sent, my thinking cleared up a bit. If you are at all interested in this project, read on, details below:
BTW, this is my FIRST project with the BS2. I can't believe I actually got it working about 1:30AM. Here's the scenario:
My company sells, services, and installs a brand of business telephone system (among a zillion other things we do related to technology). When we take over a new customer's system, we have no idea what the 4-digit code is to get into the programming on the phone switch.
The phone system has a DB9 serial port for PC hookup and programming. When you hook it up, press return in a terminal, you are presented with a password prompt. If you enter a wrong password, it simply does nothing. However, if you enter the correct code, you get a specific prompt that I wait for with SERIN.
So, I built a very simple BS2 circuit using the professional development board that simply tries every number from 0000 to 9999 with "theword" appended to the code. I have discovered that you can crack any code inside of 20 minutes, and usually shorter depending on how close the actual code is to 0000.
The code I wrote is considerably more complex than my example above, accounting for things like no response from the phone switch, using a 7-segment display to indicate working status, and percentage complete, as well as a way to display the cracked code, all on one 7-segment display.
It works like a champ, has correctly cracked every code I tried so far. Future additions might be auto-baud detect AND...(speeding up the search)
leading to my next related question which I will start in a topic of its own entitled "Guessing a number".
Post Edited (starion) : 3/4/2007 3:33:05 PM GMT
This points out something else. Almost any security system can be hacked. What the security system does is slow down the hacker (or raise flags "Hey! Somebody just tried 100 different PIN numbers!") so it takes long enough the hacker can be apprehended.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
The issue here is whether anybody (besides myself) would find any value to hacking the installer code. Only myself and possibly other dealers who sell and maintain this type of system. I have no malicious intent here, simply something that makes my job easier. I suppose someone could wreak havoc with this kind of tool.
The other consideration is that this method requires physical access to the switch. Although I am working on a software cracker to perform the same function remotely.
·· My point was directed more at Allan's comment about security.· I guess my point was, even though it is quite lax in this device, you can't expect that everywhere.· For example, in the movie The Terminator (I think 3) they show the kid hacking the ATM machine using the same exact method you're using.· That wouldn't work on any ATM I have ever seen.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Truly Understand the Fundamentals and the Path will be so much easier...