Shop OBEX P1 Docs P2 Docs Learn Events
PBasic code to convert base10 to HEX and then back to base10 — Parallax Forums

PBasic code to convert base10 to HEX and then back to base10

arnold113arnold113 Posts: 43
edited 2014-12-15 11:20 in BASIC Stamp
HELP ANYONE!!! I have a 4 digit base10 number in a reg(word). I need to convert it to a HEX number so I can seperate the highbyte into one reg and the lowbyte into another reg, do some manipulating of the two regs and then convert both back into base10 numbers. I haven't been able to get it done yet. PLEASE HELP. Thanks. Arnold

Comments

  • SapphireSapphire Posts: 496
    edited 2014-12-14 11:52
    There is no difference in the storage of a decimal or hex numbers. It's only your interpretation of the number. BCD (Binary Coded Decimal) is another story.

    If your variable is NUM, then use NUM.HighByte to get the high byte, and NUM.LowByte to get the low byte.

    You can manipulate NUM.HighByte and NUM.LowByte as you please. The result is always available in NUM.
  • arnold113arnold113 Posts: 43
    edited 2014-12-14 13:58
    If I load the reg with a hex num ($2550) then I can use highbyte to get 25 in one reg and lowbyte to get 50 in the other reg. My problem is the number is loaded into the reg as a result of a SERIN instruction as a base10 num.; I get highbyte = 9, lowbyte = 0 instead of highbyte = 25, lowbyte = 50. How can I get the num in a format that I get the 25, 50? Maybe the num is BCD instead of base10???
  • SapphireSapphire Posts: 496
    edited 2014-12-14 14:12
    SERIN doesn't have a formatter for BCD, so can you post your code with the SERIN command?

    If you got highbyte = 9 and lowbyte = 0 then your number was 2304 (decimal) or $0900 hex.

    Do you want to manipulate the 23 and the 04 parts?

    If so, then use:

    HB = NUM / 100
    > i.e. (2304 / 100) = 23
    LB = NUM // 100
    > i.e. (2304 // 100) = 04

    To put it back after manipulation,

    NUM = 100 * HB + LB
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-14 14:14
    arnold113 wrote: »
    If I load the reg with a hex num ($2550) then I can use highbyte to get 25 in one reg and lowbyte to get 50 in the other reg. My problem is the number is loaded into the reg as a result of a SERIN instruction as a base10 num.; I get highbyte = 9, lowbyte = 0 instead of highbyte = 25, lowbyte = 50. How can I get the num in a format that I get the 25, 50? Maybe the num is BCD instead of base10???

    Can you post the code you're using?

    It sounds like you're not using the correct parameters with your SERIN command.

    To post code to the forum use code tags like this:

    [noparse]
    [/noparse]
    Paste your code between the code tags.
    [color=white]__[/color]This will preserve
    [color=white]____[/color]the indentation 
    [color=white]______[/color]and formatting.
    [noparse]
    
    [/noparse]

    Without code tags the above text looks like this:

    Paste your code between the code tags.
    This will preserve
    the indentation
    and formatting.

    With code tags you get:
    Paste your code between the code tags.
      This will preserve
        the indentation 
          and formatting.
    
  • arnold113arnold113 Posts: 43
    edited 2014-12-14 14:30
    Looks like I cried wolf too soon. You suggestions got me thinking of another, easy way to get what I want. I just do: reg1 = 2550 / 100 to get the upper byte and reg2 = 2550 // 100. The 25 will displayed as inches and the 50 will be displayed as the closest sixteenth of an inch. I should have told you what I am trying to get done. Thanks for everything. Arnold
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-15 11:20
    Here are subroutines for converting the data between Decimal and BCD that I have used in clocks and other projects.
    BCD_Decimal:
      counter = (counter.NIB1 * 10) + counter.NIB0
      RETURN
                                    ' Convert BCD To Decimal
    Decimal_BCD:
      counter = (counter / 10 << 4) + (counter // 10)
      RETURN
    
Sign In or Register to comment.