PDA

View Full Version : Convert Byte to Long



DiablodeMorte
05-25-2008, 12:46 PM
I used to have a long post here about how I couldn't figure out how to make something work. I've gotten it all solved except for 1 thing.
I have 3 bytes. The first is the 100's digit, the second is the 10's digit and the third is the 1's digit. How do I convert them to 1 long? It seems that my SPIN abilities are very very rusty

Regards

PS. Here's what i mean:

SerialByte[0] = 100's digit
SerialByte = 10's digit
SerialByte = 1's digit
final = long variable i want to equal the above

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Current Projects:
Robot Control Via Skype API - Dev Stage(50% Complete) - Total(25%)
Robot Localization Via Xbee's - Research Stage
IR Tracking with Propeller - Research Stage

Post Edited (DiablodeMorte) : 5/25/2008 5:57:45 AM GMT

T Chap
05-25-2008, 02:39 PM
I am beyond rusty myself. Since it is a slow night on responses I will put out a guess and hope someone else can say if this is workable.

What about set some variables for the 3 incoming serial bytes:

Byte1
Byte10
Byte100

Also a Long to hold the sum called Final

When you read the bytes in, store them to the right byte var

Then, once you have a value in each byte:

Byte10 = Byte10 x 10
Byte100 = Byte100 x 100

Final = Byte1 + Byte10 + Byte100

Obviously use Spin syntax but this is an idea to get there.

StefanL38
05-25-2008, 05:49 PM
Hello DiablodeMorte,

you wrote "serialbyte[0]"

does this mean the three digits are inside an array?
does this mean the three digits are ASCII-Code that you send via a serial connection?

i mean: f.e. you want to send the number "458" do you send CHARACTER "4" CHARACTER "5" CHARACTER "8" ?

The CHARACTER "0" has ACII-Code 48 so you have to do ASCII-Code - 48 to get the DECIMAL value ZERO of that "character"

and then it works like in the post befor

f.e. hyperterminal sends the characters "458"

RcvDigit1 := Serial.Rx
RcvDigit10 := Serial.Rx
RcvDigit100 := Serial.Rx

so if you would send back the received byte with the .dec-function of the FullDuplexSerial-Object you would get

serial.dec(RcvDigit1) hyperterminal dislays: 52 (48 + 4)
serial.dec(RcvDigit10) hyperterminal dislays: 53 (48 + 5)
serial.dec(RcvDigit100) hyperterminal dislays: 56 (48 + 8)

DecVal1 := RcvDigit1 - 48
DecVal10 := RcvDigit10 - 48
DecVal100 := RcvDigit100 - 48

Final := DecVal100 * 100 + DecVal10 * 10 + DecVal1

this could be coded more efficient in a loop and with bytearrays

but that's the basic idea

best regards

Stefan