Parsing a 3-digit integer into three byte (hundreds, tens, digits) results ???
wperko
Posts: 66
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
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
but,
anyway.
Where do I find documentation for PBASIC "DIG" command?
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.