Shop OBEX P1 Docs P2 Docs Learn Events
Easier way to handling bytes? — Parallax Forums

Easier way to handling bytes?

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2007-10-17 10:07 in Propeller 1
I've got a routine that looks something like this...
(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

  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-16 03:13
    There's no way to do what you want directly in Spin. I've used a string compare subroutine that uses a length parameter rather than the terminating zero that strcomp uses. Something like:
    PRI textComp(str1,str2,len)
       repeat len
          if byte[noparse][[/noparse]str1++] <> byte[noparse][[/noparse]str2++]
             return false
       return true
    


    You'd do:
       if textComp(@command,string("spin"),4)
    
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-10-16 03:14
    Ah.. Six one way... A half dozen the other.. Too bad.. [noparse]:)[/noparse]

    Thanks!

    Oldbitcollector

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

    — Calvin, of 'Calvin and Hobbes.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-10-16 03:19
    That does make the code look a bit less sloppy..

    Thanks again..

    Oldbitcollector

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Buttons . . . check. Dials . . . check. Switches . . . check. Little colored lights . . . check.

    — Calvin, of 'Calvin and Hobbes.
  • mirrormirror Posts: 322
    edited 2007-10-16 04:02
    Oldbitcollector said...
    I've got a routine that looks something like this...
    (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

    if command == constant("s" + ("p" << 8) + ("i" << 16) + ("n" << 24)))

    ·
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-10-16 04:11
    mirror,

    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 AllenTracy Allen Posts: 6,660
    edited 2007-10-16 04:28
    like mirror's,
    CON
      spin = "s" + ("p" << 8) + ("i" << 16) + ("n" << 24)
      pins = "p + ("i" << 8) + ("n" << 16) + ("s" << 24)
      nips = "n + ("i" << 8) + ("p" << 16) + ("s" << 24)
      snip = "s + ("n" << 8) + ("i" << 16) + ("p" << 24)
    VAR 
      word command  ' will be built from 4 bytes
    
    PUB ...
    if command == spin ...
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-16 06:00
    Just a typo in line 7: LONG not WORD

    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 smile.gif

    Post Edited (deSilva) : 10/16/2007 6:13:32 AM GMT
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-10-16 15:44
    Why not just figure out what the numerical value of your match ("spin") and do a compare? $7370606e aka 1,936,744,558
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2007-10-16 16:46
    Oh yes, sorry, LONG not WORD.

    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
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-10-16 16:49
    @Tracy / @Fred

    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.
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-16 16:52
    I am sure Tracy or Fred will do it smile.gif
    However the math is mostly based on adding and shifting....
  • TheWizard65TheWizard65 Posts: 91
    edited 2007-10-16 22:08
    DUUUUUUUUUUUhhhhh, looks like im in the wrong room. Is this any were close to basic. I mean the programming language that is. Im even newer than oldbitcollector. Im just trying to get a handel on this. I was taught by my Dad, that the Only dumb question, is the one that is never asked.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-16 22:59
    TheWizard65,
    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.
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-10-16 23:23
    Wizard, its not as bad as it looks, they are just loading charectors into a single long (32 bit number) by shifting them to the correct position before adding them up.

    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
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-10-16 23:48
    Hang in there Brad! Resources are on the way..

    (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.
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-10-17 04:34
    Oldbitcollector,

    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
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2007-10-17 04:43
    I incorporated Mike's solution to this problem into my PropDOS, which was the
    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.
  • JavalinJavalin Posts: 892
    edited 2007-10-17 09:22
    but you can use the:
    if strcomp(@string_var,string("on"))
    

    SPIN command.· Its the same as Mikes·compare routine I believe.

    J
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-17 10:07
    strcomp needs strings for both parameters, needing a zero termination. So you have to care for something as
    BYTE[noparse][[/noparse]@string_var+2] := 0
    


    before it will work
Sign In or Register to comment.