Converting Decimal value to Hex for serout

Hooter

Senior Member
Folks - I need help.
I have a Dorji DRF5150 transmitter connected to a DS18b20 which transmits to a Dorji DRF4432S.
The DRF4432S receiver then sends a 6 byte string of decimal values to a Picaxe 20x2 for processing.
This all works very well.
I have recently purchased a 4D systems uLCD-35DT display module (nice piece of gear
and so easy to use) which requires hex values to be inputted via its serial port to update
and adjust the settings of the temperature gauge I have created for the display.
I only need to convert one of the six decimal bytes sent to the 20x2 by the receiver, and insert it into the 5th byte
of the 6 byte string to send to the display.
My problem is working out this conversion process.
I have tried a couple of things to no avail and I have got my thinking process into an endless loop.
Can anyone suggest a method I could use for this conversion?
Any assistance is appreciated.
Hooter
 

srnet

Senior Member
Code:
hextoascii:
	'converts var1 byte variable to 2 ASCII hex characters, ascii characters returned in var1 and var2

	var2 = var1 AND $0F 			'isolate bottom nibble
	if var2 < 10 then
		var2 = $30 + var2
	else
		var2 = var2 + 55
	endif

	var3 = var1 AND  $F0 			'isolate top nibble
	var3 = var3 / 16				'moves top 4 bits into bottom 4 bits

	if var3 < 10 then
		var3 = $30 + var3
	else
		var3 = var3 + 55
	endif

	var1 = var3
return
 

inglewoodpete

Senior Member
Variety is the spice!

This is the subroutine that I use when I need hexadecimal output. The SerTxd can be replaced with hSerOut or SerOut where required.

Code:
'
' ----- ShowHex: Send Hexadecimal value to PE Terminal -------------------------------
'
'         ShowHex:  Send a  byte  as hex to terminal on the programming lead
'         SHexNibl: Send a nibble as hex to terminal on the programming lead (Bits 0 to 3)
'         ShowHexD, ShowNibD: Alternate "D" entries cause a leading $ sign to be sent
'
'         Entry:   bShowByte   (byte) data to be transmitted
'         Exit:    bShowByte   (byte) unchanged
'         Used:    bTemp       (byte)
'
ShowHexD:SerTxd("$")                      'Alternate entry (Display Byte)
ShowHex: bTemp = bShowByte >> 4           'Entry - Display byte. Get high nibble
         GoSub SHexNib2                   'Show it
         GoTo SHexNibl
ShowNibD:SerTxd("$")                      'Alternate entry (Display nibble)
SHexNibl:bTemp = bShowByte And mskLoNibble'Entry - Display nibble
SHexNib2:bTemp = bTemp + "0"              'Convert to ASCII
         If bTemp > "9" Then              'Separate out and
            bTemp = bTemp + 7             ' correct over-decimal part.
         EndIf
         SerTxd(bTemp)                    'Display it
         Return
 

Goeytex

Senior Member
I have recently purchased a 4D systems uLCD-35DT display module (nice piece of gear
and so easy to use) which requires hex values to be inputted via its serial port to update
and adjust the settings of the temperature gauge I have created for the display.
I only need to convert one of the six decimal bytes sent to the 20x2 by the receiver, and insert it into the 5th byte
of the 6 byte string to send to the display.
It does not matter to the display what format the data is in. If you are using the serial interface the Display does not know the difference between hserout 0, (121) ... hserout 0,($79) or hserout 0, (%01111001). The same exact same data is sent to the display in each case.

For example, if you want to set an object color to green you would send: hserout 0,(0xFF, 0x83, 0x00, 0x12, 0x04, 0x00)
However. You could also send: Hserout 0, (255, 131, 0, 18, 4, 0)

The display does not know the difference

Converting from dec to hex is for displaying the data on a screen or terminal in that particular format.
 
Last edited:

Hooter

Senior Member
Thanks for the responses guys.
Goeytex you are quite right again - I don't know how I missed that.
All is working as it should now.
Thanks Srnet and Inglwoodpete for taking the time to respond to my confusion.
Cheers
 
Top