Easier way to handling bytes?
Oldbitcollector (Jeff)
Posts: 8,091
I've got a routine that looks something like this...
(Checking for only the first four characters)
It works, but looks messy.. Is there a way to do something like this?
Thanks
Oldbit
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
(Checking for only the first four characters)
if command[noparse][[/noparse]0] == "s" and command == "p" and command == "i" and command == "n"
It works, but looks messy.. Is there a way to do something like this?
if command[noparse][[/noparse]0..3] == "spin"
Thanks
Oldbit
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
Comments
You'd do:
Thanks!
Oldbitcollector
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
Thanks again..
Oldbitcollector
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
·
Your method will work if command is defined as a Long, with the characters packed lsb first. If it's defined as a Byte array, command by itself is the same as command[noparse][[/noparse]0], which returns a single character.
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
And note that it is NOT a type, when Tracy seemingly arranges the characters right to left in the constants. Just take it that little endians need some doublethink
Post Edited (deSilva) : 10/16/2007 6:13:32 AM GMT
Fred, that is what the constant definition is doing,
spin = "s" + ("p" << 8) + ("i" << 16) + ("n" << 24)
is the same as $6E697073. No extra program space required, but lets the compiler do the work.
$6E697073 could just as easily represent "spin" or "nips", depending on how the bytes are built up to form the long, big or little endian.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Would you guys mind explaining your math a little more..
(Remember: I'm a beginner.. [noparse]:)[/noparse]
>$6E697073
Is it S=6E P=69 I=70 N=73 (hex keycodes?)
Thanks
Oldbitcollector
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
However the math is mostly based on adding and shifting....
What's your question (other than "where am I")?
The Propeller is programmed in a language called SPIN which is similar in many ways to C, but is indeed different. It can also be programmed in the Propeller's assembly language. There is a very simple Basic interpreter that runs on a Propeller that can be downloaded and installed on any of the Propeller "boards".
The Propeller is a 32-bit integer computer cluster (of 8 processors). All arithmetic is done on 32-bit integers although there is a floating point package available and the compiler will compile floating point constants, but otherwise doesn't know about floating point. The floating point package uses function calls for everything.
Start with the Propeller Education Kit tutorials and have the Propeller Manual on hand for reference. The Hydra Manual is also excellent, but is not free for downloading as the others are.
Spin's really easy for a great many things but it's worth learning plenty about binary/hex to really understand what you are doing.
Graham
(Sooner or later I'm going to start that .spin for BASIC programmers text)
*almost* ready..
Oldbitcollector
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
My 'math' consisted of looking up the hex value of the letter in an ascii table.
So, for me (which is probably incorrect order in a little-endian world) it was: 's'= $73, 'p'=$70, 'i'=$60, 'n'=$6e
Then I told the calculator to convert to decimal and copied the decimal value.
Later my middle bullmastiff chewed the + key off my other calculator and we had words.
However, my implied point was and still is that: 'Yes, programs can be made to jump through little tiny hoops, but if you prefigure your problem out in the real world, a simpler way may be possible'.
Which applied here, just make your variable equal to the known value of spin (either my way or Terry's, pick the one that works) and compare to command[noparse][[/noparse]0]. I suppose the real question is: What value is in command[noparse][[/noparse]0] and why isn't it a simple number?
Fred
reason for the question. This provided the first part of command-line support.
I am pleased by all the responses, this thread has become quite educational!
Oldbitcollector
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.
— Calvin, of 'Calvin and Hobbes.
SPIN command.· Its the same as Mikes·compare routine I believe.
J
before it will work