Shop OBEX P1 Docs P2 Docs Learn Events
Parsing a 3-digit integer into three byte (hundreds, tens, digits) results ??? — Parallax Forums

Parsing a 3-digit integer into three byte (hundreds, tens, digits) results ???

wperkowperko Posts: 66
edited 2013-07-19 06:44 in BASIC Stamp
Hi,

My brain so far has gone blank on this ... using PBASIC how can I parse a three digit integer into three separate bytes ... one for the Hundreds, one for the Tens and one for the single digit column as say H, M, L variable names?

Hence if I have a number 726 I would end up with H=7, M=2 and L=6
or
Hence if I have a number 358 I would end up with H=3, M=5 and L=8 ... and so on ...


As dumb as this seems I think my brain is graying out ... there should be a simple little subroutine to do this!


Here is the code I'm using ... what's wrong with it?

' ByteParse3.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
' Initialze
'
' Variables
'
ibyte VAR Byte ' byte to be converted
hbyte VAR Byte ' bytes to represent Hundreds digit
mbyte VAR Byte ' bytes to represent Tens digit
lbyte VAR Byte ' bytes to represent Singles digit
Remainder VAR Byte
' =========================================================================
' PROGRAM Start
ibyte = 768 ' initial byte to be converted to 3 ASCII byte values

DEBUG CR, CR, "ibyte = ", DEC ibyte, CR
hbyte = ibyte/100 ' hundreds value as integer
Remainder = (ibyte - (hbyte * 100))
DEBUG "Remainder = ", DEC Remainder, CR
mbyte = (Remainder/10) ' divide hundreds remainder by 10; integer value
Remainder = (Remainder - (mbyte * 10))
DEBUG "Remainder = ", DEC Remainder, CR, CR
lbyte = Remainder ' remainder after dividing by 10

DEBUG CR, "hbyte = ", DEC hbyte, CR
DEBUG CR, "mbyte = ", DEC mbyte, CR
DEBUG CR, "lbyte = ", DEC lbyte, CR

END

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-07-18 19:01
    H = a_number / 100
    M = (a_number//100) / 10
    L = (a_number//100) // 10
    
  • ElectrodudeElectrodude Posts: 1,646
    edited 2013-07-18 19:19
    The above code will work but if you don't want unexpected things happening if the number is =>1000 and if you want a slight optimization then try this:
    H = (a_number // 1000) / 100
    M = (a_number // 100) / 10
    L = a_number // 10
    
    Anything higher than 1000 will have the top digits (that I assume you don't want) to get thrown out (1234-> H=2; M=3; L=4) instead of 1234-> H=12; M=3; L=4
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-07-18 19:24
    The Subject is "Parsing a 3-digit integer...",
    but,
    anyway.
  • SapphireSapphire Posts: 496
    edited 2013-07-18 20:37
    Or, use the DIG command:
    hbyte = ibyte DIG 2
    mbyte = ibyte DIG 1
    lbyte = ibyte DIG 0
    
  • wperkowperko Posts: 66
    edited 2013-07-19 05:16
    Hi,

    Where do I find documentation for PBASIC "DIG" command?


    Sapphire wrote: »
    Or, use the DIG command:
    hbyte = ibyte DIG 2
    mbyte = ibyte DIG 1
    lbyte = ibyte DIG 0
    
  • wperkowperko Posts: 66
    edited 2013-07-19 05:29
    Hi,

    That is a great answer. I've never heard of the "DIG" command and it's not in my "BASIC Stamp Syntax and Reference Manual" either.

    But this is definitely the best way to solve the problem in PBASIC!

    Sapphire wrote: »
    Or, use the DIG command:
    hbyte = ibyte DIG 2
    mbyte = ibyte DIG 1
    lbyte = ibyte DIG 0
    
  • Hal AlbachHal Albach Posts: 747
    edited 2013-07-19 06:44
    wperko wrote: »
    Hi,

    That is a great answer. I've never heard of the "DIG" command and it's not in my "BASIC Stamp Syntax and Reference Manual" either.

    But this is definitely the best way to solve the problem in PBASIC!

    DIG or Digit is not a command, it is an operator like + or -. In the Basic Stamp Manual Ver 2.0 it can be found on pages 66 and 72. In the most recent manual 2.5.3 it is on pages 109 and 117.
Sign In or Register to comment.