PDA

View Full Version : Best way to join two variable in one



Alberto
- 27th January 2017, 10:51
I am downloading some data from a cloud, collecting the ascii characters in a byte array. Now I need to use some of the numbers received, so I have to move them into a word variable.

What I am doing at the moment is:

W0 = ((b0-48)*100) + ((b1-48)*10) + (b2-48)

Where:

b0 = "6"
b1 = "5"
b2 = "3"

W = 653

Is there a better way to covert ascii to number?


Alberto

Edited:
Why the thread title cannot be edited?

Dave
- 27th January 2017, 11:33
Alberto, The way you are proposing is just fine. I use a method similar when entering ascii text numeric like this:
CASE $30,$31,$32,$33,$34,$35,$36,$37,$38,$39 '0,1,2,3,4,5,6,7,8,9
TARGETEMP = TARGETEMP * 10 'NUMERIC KEY JUST RELEASED SO ADD TO VALUE
TARGETEMP = TARGETEMP + (CHARACTER - $30)

That way you can enter as many numeric as possible. (up to 10 if using LONGS)

Alberto
- 27th January 2017, 12:14
That way you can enter as many numeric as possible. (up to 10 if using LONGS)



Thank you Dave for the answer. I thought there cold be a magic way like when converting a variable into ascii using "dec".

I never used long (I don't even know how to commence) can you give me some hint?

I am running short of memory space ( using pic18f2620) could long create memory problem?

Thank you again for your support.

Alberto

Charlie
- 28th January 2017, 14:38
What is the biggest number you want to convert? if it's less than 65,536 then WORDs are fine...

Alberto
- 29th January 2017, 09:10
I need to convert 24 Hours in seconds, so the number span from 1 to 86400 which cannot be handled by a word. Now I am using the workaround using a 12 Hours system, which complicate a bit the coding, since the source time base is 24 Hours.

Long could be a solution, but I need a break on the coding and learn how to use the long and then, with the knowledge, decide if use it or leave the things as they are. (After all the workaround works fine!)

I was asking for a snippet on how to use the long, good starting point in the learning process.

Thank you anyway.

Allberto

Edited: I did search into pbp3 user manual and didn't find any reference to long! Where have I to search?

richard
- 29th January 2017, 09:58
compiling for longs is wasteful of resources and slows your prog down . if you don't really need them don't do it , there are many ways to avoid it .
start here
http://www.picbasic.co.uk/forum/showthread.php?t=24&highlight=div32

Alberto
- 29th January 2017, 11:13
Thank you Richard, very interesting link. I thing I will try to use Darrel macro for big number!

Alberto