coding ascii math problem
lfreeze
Posts: 174
Please help, I have a basic coding problem that I can not solve. If you run the inserted program, it will compile and run correctly.
It is a simple program to convert ascii numbers to the integer values. If you then add another variable AAAA to the end of The line:
PUB CONVERT | temp,temp1,temp2,AAAA
The program will compile and run, but now produces the wrong result,
In my tests it will continually show 3 as the converted value. The puzzling part is
That if you add the additional variable AAAA to the front of the statement such as:
PUB CONVERT | AAAA,temp,temp1,temp2
The program will run correctly.
Thanks
Larry
It is a simple program to convert ascii numbers to the integer values. If you then add another variable AAAA to the end of The line:
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 obj ser: "fullduplexserial" PUB CONVERT | temp,temp1,temp2 ' ,AAAA ser.start(31,30,0,19200) temp:=55 'THIS IS THE ASCII VALUE TO CONVERT repeat waitcnt (clkfreq/2 + cnt) ser.str(string(13,13," ascii = ")) ser.dec(temp) 'this is the ascii value temp1 := temp * 10 + (temp - 48 ) 'this and next line converts ascii nbrs to decimal temp2:=temp1[3] 'this eXtracts the third byte of the temp1 long ser.str(string(13," converted to decimal = ")) ser.dec(temp2) 'this is the converted value
PUB CONVERT | temp,temp1,temp2,AAAA
The program will compile and run, but now produces the wrong result,
In my tests it will continually show 3 as the converted value. The puzzling part is
That if you add the additional variable AAAA to the front of the statement such as:
PUB CONVERT | AAAA,temp,temp1,temp2
The program will run correctly.
Thanks
Larry
Comments
You can try it with this:
I think the problem you're having is that temp is not an ASCII value, it's already a number.
Look at Jon's sample code and try to understand what it does. Please ask more questions if it still doesn't make sense.
This very simple code creates a randomized, slightly-wobbly reverse-sawtooth waveform on eight of the DMX channels -- with the LEDs and smoke effect being used, it provides a nice simulation of flames. DMX is a byte-oriented system, but local vars in methods are always longs. Note how I declared a local array of two longs (level[]) but treat it as an array of eight bytes.
For those interested, the p_dest value is a pointer into the DMX output array. This strategy lets me direct the "flames" animation to any output I desire. In this case, it's the DMX stream. While testing, I used local outputs on the HC-8+.
comments made by Dave, i am still confused about why there is no third byte in the result of my original
program. I will spend some more time with the manual and try to expand my understanding.
Larry
Other issues:
-- locals are not initialized by the compiler; you could end up with junk in them, depending on where the routine is called.
-- Technically, temp1[3] is undefined; temp1 is the same as temp1[0], temp2 is the same thing as temp1[1] -- temp1[3] is an unknown from the stack