How do I combine 2 ascii characters into 1 hex value?
T&E Engineer
Posts: 1,396
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:
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
Here's the code for it:
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:
After this line bcdVal will contain $25 (decimal 37)
Great job - Recommend this should go into SX/B 2.0 ... as others are to come...
Thanks again!