Hex conversion in SX/B ??
I am looking for a way to convert a variable into it's HEX value.
FOR EXAMPLE.....
Value···· VAR·· Byte(8)
' Value(1) = 23
Value(1) = $23····· ' is valid
but
' Value(1) = 23
Value(1) = $Value(1)··' is not·valid
Am I missing something or is a function required to do this?
If so, is anyone willing to share one?
Thanks.
FOR EXAMPLE.....
Value···· VAR·· Byte(8)
' Value(1) = 23
Value(1) = $23····· ' is valid
but
' Value(1) = 23
Value(1) = $Value(1)··' is not·valid
Am I missing something or is a function required to do this?
If so, is anyone willing to share one?
Thanks.
Comments
You want to divide the value by 10, then multiply by 16, then add in the original value modulus 10.
What you are doing is called BCD conversion. Do a search there is probably a post about it in the forums.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"
Benjamin Franklin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
I looked through every title under the SX section over the last few years for "HEX" and only came up with 1 or 2.
I did see one reference to a WORD HEX COUNTER that you wrote. Here is a couple of subroutines:
GET_HEX· SUB 2····· ' print using HEXx format
LCD_OUT· SUB 1, 2·· ' send to LCD
'
'
'
' Subroutines / Jump Table
'
LCD_OUT:
· temp1 = __PARAM1······························ ' digit to send
· SEROUT Lcdout, LCDBaud, temp1················· ' send the digitacter·
· RETURN
GET_HEX:·····
· wTemp1 = __WPARAM12····························· ' copy output value
· temp1 = wTemp1_MSB >> 4
· READ Hex_Digit + temp1, temp1
· LCD_OUT temp1
· temp1 = wTemp1_MSB & $0F
· READ Hex_Digit + temp1, temp1
· LCD_OUT temp1
· temp1 = wTemp1_LSB >> 4
· READ Hex_Digit + temp1, temp1
· LCD_OUT temp1
· temp1 = wTemp1_LSB & $0F
· READ Hex_Digit + temp1, temp1
· LCD_OUT temp1
RETURN
'
Hex_Digit:
· DATA "0123456789ABCDEF"
'
[/code][/size]
However, it is written for a WORD not a byte (which gets me back to LSB and MSB of Bytes (aka..nibbles for the SX which doesn't exist).
However, in the SX DS1302 program written by JDOhio and my modification there is a routine which may also be useful:
I modified it a little but it will read in a byte (e.g. $5A) and give out $35 and $3A so they can be in ASCII format (which I believe is what the RobOympic "StringWriter" routine (I am using) looks for).
I also found this DEC2BCD and BCD2DEC routine that JonnyMac wrote (but I have not been able to figure out how to test it yet):
http://forums.parallax.com/showthread.php?p=640449
What do you think about these - Is this right or do I go after the BCD approach?
Thanks.
Post Edited (T&E Engineer) : 4/4/2007 2:45:40 PM GMT
Thanks again.
PS: I saw your website last night and your movie clips. Pretty impressive!
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"
Benjamin Franklin
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
0 in gives $00 out
30 in gives $30 out
99 in gives $99 out
What I need is a straight hex conversion not BCD it appears.
0 in gives $00 out
30 in gives $1E out
99 in gives $63 out.
I think what Bean said it what I need to do.
What does "add the original value modulus 10" mean?
Is it this?··
Value···· VAR···· BYTE
Value2·· VAR···· BYTE
Value2 = Value
Value = Value / 10
Value = Value * 16
Value = Value + Value2 // 10 I also tried this routine but it is not right either. What is wrong? It is only looking at 1 digit I think.
Start:
· FOR inVal = 0 TO 99
··· outVal = DEC2HEX inVal
··· \ WATCH inval, 8, udec
··· \ WATCH outval, 8, uhex
··· BREAK
· NEXT
· GOTO Start
DEC2HEX:
· tmpB1 = __PARAM1····························· ' get parameter
· tmpB2 = tmpB1 / 10··························· ' isolate 10s
· tmpB2 = tmpB2 * 16
· tmpB2 = tmpB2 + tmpB1
· tmpB2 = tmpB2 // 10
· READ Hex_Digit + tmpB1, tmpB2
· RETURN tmpB2
Hex_Digit:
· DATA "0123456789ABCDEF"
Post Edited (T&E Engineer) : 4/4/2007 11:27:26 PM GMT
This might work for you. (I have not actually tested it!)
- Sparks
I see that the byte separations (nibbles) are to be "displayed". What I would like is to combine them back together so that tmpB2 actually comes back with "1E" if I give it a 30 in. Howeer, I dont know if that means I need to AND or OR the nibble halves together.
I will try this out tonight and let everyone know.
Post Edited (T&E Engineer) : 4/5/2007 5:27:22 PM GMT