combining variables (cancatenation)
lfreeze
Posts: 174
Using spin is it possible to combine variables as follows:
···· variable1:=5
···· variable2:=6
I want to combine these into a result that· will equal···· variable3:=56······· I've done searches on the forum and don't see
this question. I have tried the manual and find no reference to it.
Thank you in advance for any responses
Larry
···· variable1:=5
···· variable2:=6
I want to combine these into a result that· will equal···· variable3:=56······· I've done searches on the forum and don't see
this question. I have tried the manual and find no reference to it.
Thank you in advance for any responses
Larry
Comments
Perhaps you're trying to do something more complicated?
You'll get a more useful answer if you explain more about what you want to do.
variableA:=A
variableB:=B
I am trying to get a new variable from variableA and variableB that will equal···· variableC:=AB
Larry
It's concatenate by the way.
Graham
Combine a+b with shift a 8 bits to high order in word of variable a
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nothing is impossible, there are only different degrees of difficulty.
Sapieha
I ·want to use numbers, I used letters in the example previous example to make the question easier to understand.· here are a few examples of· what I am trying to accomplish. there is no math function involved just putting the orginal variables side by side in a new variable
···· variable1:=1
· ·· variable2:=1
I want variable3 to equal· ·11
·· variable1:=4
·· variable2:=5
I want variable3 to equal 45
·· variable1:=8
·· variable2:=7
I want variable3 to equal 87
Thanks
Larry
This is typical BCD variable.
Combine it in 2 Nibles
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nothing is impossible, there are only different degrees of difficulty.
Sapieha
You have got a perfectly valid answer from Mike, bt you are still repeating your - most like incomplete and misunerstandable - examples.
I have the shrude idea that you are after some string concatenation, but this is not at all obvious.
DeSilva - I am pretty sure that is what he means ..
Regards,
John Twomey
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Necessity is the mother of invention'
question other than by providing examples of what I am looking ·for.· If· ·I concatenate the number 1, and
the number 2, I get 12.·· I was trying to show this in my previous examples, obviously I failed.
Thank you all again I hope this adds some clarity to my question.
Larry
variable3 := variable1 * 10 + variable2.
if variable1 = 1 and variable2= 2 then variable3 = 1*10+2 = 12
Graham
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
So tell us, please, what is your final objective here?
-Phil
Problem is only att.
1 = 0001 - Binary in Nible BCD
1= 00000001 - in Byte binary
and
"1" = 00110001 in ASCI(string) in byte binary.
finaly you
variable1:=4
variable2:=5
I want variable3 to equal 45
4 = in ASCI - 0011_0100
5 = in ASCI - 0011_0101
If you mask high Nible on it
4 = 0100
5 = 0101
variable3 to equal 45 = in Byte 0100_0101
In Nibles
(0100 <<4) + 0101 = Byte 0100_0101
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nothing is impossible, there are only different degrees of difficulty.
Sapieha
Var 1 = Byte = 4
Var 2 = Byte = 5
'Var3 := (Var1 << 8) & Var2' works for me [noparse]:)[/noparse]
This wouldn't give you '45' as a number, but would give you a '4' beside a '5' in the same word/long. Is that what you mean?
Same as what Sapieha is saying I believe?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Alec
My our page
Thank you for taking the time to reply. I am becoming very frustrated with my inability to express my questions correctly I didn't add my final objective to the previous posts as I didn't want to add any additional confusion. My final objective with this project is to receive·single serial digits from a keyboard·into the propeller. I am trying to combine (concatenate) these random digits into groups of two digits as a single variable as soon as they are received by the propeller.
·I don't want to do any math with them, simply concatenate them, and then to export them to a BS2.· I am looking for a simple way to do this. I now think, that is not possible. I was hoping I could avoid writing a· large (if then) piece of code. such as
if variable1 = 1 and variable2 =2·· then variable 3 = 12
·if variable1 = 1 and variable2 =3·· then variable 3 = 13
if variable1 = 5 and variable2 =4·· then variable 3 = 54
ETC. ETC.
My original plan is to use incoming digits,· and pass them back out to a bs2 which will be set up to· take specific actions based on the variables. It is a robotic application with each number providing some form of· control.··
Larry
That helps a lot, since it tells me you want two digits "packed" (perhaps a better word than "concatenated") into a byte. That's easy using the BCD method:
pack := (v1 << 4) + v2
mentioned above. The BS2 would then unpack them as follows:
v1 = pack >> 4 : v2 = pack & $0F
-Phil
--- original posting ---
If the digits are coming in 1 at a time, do something like so:
set Total = 0
set NewNum = 0
Assume "Total" = what you have so far
Assume "NewNum" is the new digit that is arriving
Each time a new number arrives,
Total = (Total * 10) + NewNum
For example:
If total was zero, and 1 arrived, we would have (0 * 0) + 1 = 1
If total was 1, and 2 arrived, we would have (1 * 10) + 2 = 12
If total was currently 12 and 7 arrived as a new digit, the math would make it 12 * 10 giving us 120. We then add the new number giving us "Total = 127"
Does the above appear to fit your desired input/output?
Simple, fast and no strings or bit flipping involved.
That is very similar to what I have done in my thing.
Using 'Var3 := (Var1 << 8) & Var2' I combined Var1 with Var 2 into Var3 which I then sent as a single variable into a function.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Alec
My our page
You method works BUT.
You not have spare memory.
You only fill Word with 2 Byte
In Nible method 2 Byte numbers have one byte place.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nothing is impossible, there are only different degrees of difficulty.
Sapieha
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Alec
My our page
If:
VarA := 123
VarB := 567
then
VarC := ????
Only you can answer this question, and your answer will determine the level of help that we can give to you.
if VarC == 123567, then a possible solution to your problem is:
VarC := Pow10(1+Int(Log10(VarB))*VarA + VarB
where:
Pow10(v) := v to the power of 10
Log10(v) := the base 10 logarithm of V
Int(v) := the integer part of v i.e. drop the fraction.
That's a horribly complicated solution though!
I'd probably write the solution·as:
·
·
Post Edited (mirror) : 9/27/2007 10:21:11 PM GMT
Larry
.