PDA

View Full Version : hell of a problem using data types or adding an array



grasshopper
05-22-2008, 11:25 PM
Basically i am trying to take an array that a user can change by storing a baud rate in EEPROM. My problem is that if i add up the string that the user inputs i can seem to get it to be a correct value.
here is an example if a user inputs 9600





FLASH[ 2 ] := ((RxString[ 9 ] * 1000) + (RxString[ 10 ] * 100) + ( RxString[ 11 ] * 10) + (RxString[ 12 ]))





In the code the array Flash[ 2 ] is where i would expect to find 9600 logicaly i thought adding RxString [ 9 - 12] but it is not the case

9600 where the 9 is RxString[ 9 ] and the 6 is RxString[ 10 ] and the 0 is RxString [ 11 ] and the 0 is RxString [ 12 ]

My other foreseeable problem is that if a user inputs a number 19200 then the code will have to be modified





FLASH[ 2 ] := ((RxString[ 9 ] * 10000) + (RxString[ 10 ] * 1000) + ( RxString[ 11 ] * 100) + (RxString[ 12 ] *10 ) + (RxString[ 12 ] ))





I could address this with an If statement but some help would be nice

grasshopper
05-23-2008, 01:59 AM
Any one ?

Did i explain it enough?

I really need help on this one

Mike Green
05-23-2008, 02:22 AM
You basically want to convert a digit string into a binary value. There is already a routine (rxDec) in the Extended FullDuplexSerial object that does this. You could also look at the "getAnyNumber" method near the end of FemtoBasic.spin which allows for decimal, hexadecimal, and binary numbers.

allanlane5
05-23-2008, 03:00 AM
{
You've forgotten that the "Character" recieved will be in ASCII. The ASCII character for "0" (zero) is 30.
}
PUB GetNum(InStrAddr) : Result | I, MyLen
I := 0
MyLen := STRSIZE(InStrAddr)
repeat I from MyLen TO 0 STEP -1
Result := Result * 10
Result := Result + InStrAddr[I] - 30
RETURN Result

grasshopper
05-23-2008, 03:16 AM
allanlane5 said...


PUB GetNum(InStrAddr) : Result | I, MyLen
I := 0
MyLen := STRSIZE(InStrAddr)
repeat I from MyLen TO 0 STEP -1
Result := Result * 10
Result := Result + InStrAddr - 30
RETURN Result




Nice work thanks a million!!

This is what I was looking for .
http://forums.parallax.com/images/smilies/tongue.gif

StefanL38
05-23-2008, 04:06 AM
Hello,

the ASCII-Code for "0" zero is DECIMAL 48 and hexadecimal this is 30

a number without a leading "$" is interpreted as DECIMAL

so code has to be




PUB GetNum(InStrAddr) : Result | I, MyLen
I := 0
MyLen := STRSIZE(InStrAddr)
repeat I from MyLen TO 0 STEP -1
Result := Result * 10
Result := Result + InStrAddr - 48 'intead of 30
RETURN Result





best regards

Stefan

Post Edited (StefanL38) : 5/22/2008 9:29:47 PM GMT