Shop OBEX P1 Docs P2 Docs Learn Events
How do I combine 2 ascii characters into 1 hex value? — Parallax Forums

How do I combine 2 ascii characters into 1 hex value?

T&E EngineerT&E Engineer Posts: 1,396
edited 2008-08-02 23:27 in General Discussion
I'm getting a bit confused and stuck on ascii and hex character values in converting them.

If I have 2 ascii characters stored in bytes:
m_lin1(6)·= "2" (which is $32 for the ascii 2), and·m_lin1(7) =·"5" ($35 is ascii value of 5)
and I want to combine them into 1 hex value like:
TimeDate(Secs) = $25 , How do I do this with SXB ?

I thought of something like this but it is not right:

m_lin1(6) = m_lin1(6) - $30
m_lin1(7) = m_lin1(7) - $30

m_lin1(6) = m_lin1(6) * 10
TimeDate(Secs) = m_lin1 + m_lin2

This is for a DS1302 project where TimeDate(Secs) is normally set to a hex value. However, in this case I have ascii character values of numbers between 0 to 9 ($30 to $39). I have code to go from 1 hex value into 2 ascii values·but not combining 2 ascii values into to 1 hex value.

This is what I have and I want to do the opposite I believe:
TimeDate(Secs) = $25

HEXUPDATE1 TimeDate(Secs), PosSecs                   ' Updates the seconds display

SUB HEXUPDATE1
' USAGE: HEXUPDATE HexValue, Message1Position
' EXAMPLE: HEXUPDATE $25, 8                    ' Updates the seconds display

  tmpB3(0) = __PARAM1                          ' get hex value as first parameter
  Idx = __PARAM2                               ' get message position as second parameter
  tmpB3(1) = tmpB3(0) AND $F0                  ' isolate 1st hex value
  SWAP tmpB3(1)                                ' swap the nibbles
  READ Hex_Digit + tmpB3(1), tmpB3(1)          ' read the ASCII hex character
  m_lin1(Idx) = tmpB3(1)                       ' display the 1st hex character
  INC Idx                                      ' increment to the next message position
  tmpB3(1) = tmpB3(0) AND $0F                  ' isolate 2nd hex value
  READ Hex_Digit + tmpB3(1), tmpB3(1)          ' read the ASCII  hex character
  m_lin1(Idx) = tmpB3(1)                       ' display the 2nd hex character
  ENDSUB
 
Hex_Digit:
  DATA "0123456789ABCDEF",0

Comments

  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-02 03:06
    Define a function:

    MAKE_BCD        FUNC    1, 2
    


    Here's the code for it:

    ' Use: result = MAKE_BCD char1, char2
    
    FUNC MAKE_BCD
      __PARAM1 = __PARAM1 - "0"                     ' reduce to digit
      SWAP __PARAM1                                 ' move to high nib
      __PARAM2 = __PARAM2 - "0"                     ' reduce to digit
      __PARAM1 = __PARAM1 | __PARAM2                ' combine nibs
      RETURN __PARAM1
      ENDFUNC
    


    Note that this function doesn't require the use of any user variables, it can accomplish what you need using SX/Bs internal vars. You call it like this:

    bcdVal = MAKE_BCD "2", "5"
    


    After this line bcdVal will contain $25 (decimal 37)
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2008-08-02 23:20
    Thanks JonnyMac. That did the trick. I would not have thought to AND them together to combine them.
    Great job - Recommend this should go into SX/B 2.0 ... as others are to come...

    Thanks again!
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-02 23:27
    SX/B 2.0 features are locked and, frankly, it's not often that one wants to take two ASCII characters and combine them into a BCD value -- using a function as above is the best solution, and demonstrates SX/B's flexibility and power.
Sign In or Register to comment.