View Full Version : Byte to Hex
enigma
- 1st July 2017, 16:54
Hi all
I`m grabbing the serial number from an iButton in 6 array bytes. The full number is 12 digits, 2 per byte. Normally I output the number by serial using the HEX2 prefix to give 00 to FF from each byte. I need to write this number now to a OSD I.C which requires 12 separate bytes of data. I`ve looked at this for an age and I`m sure there must be a simple way to get two hex bytes from a single byte value but I cant discover it.
Example, the last 2 bytes returned from the iButton may be in Hex 2B 1A. I need this expressed in 4 bytes 2, B, 1, A Any ideas?
Regards Pete
aerostar
- 1st July 2017, 20:41
Off the top of my head........untested !!!!!!
Use nibbles
value = 2B
nibble = value >>4
go do lookup
nibble= value & %1111
go do lookup
findascii
Lookdown nibble,["0123456789ABCDEF"],asciichar
so
2B is binary %101011
shift right 4 =%0010
do the look down which returns 2 in ascii
nibble= value & %1111 = %1011
do the look down which returns B in ascii
Lookdown lownibble,["0123456789ABCDEF"],asciichar
Dave
- 2nd July 2017, 15:35
enigma, Here is a subroutine I use to output ascii equivalent data for a word in hex:
'************************************************* ********************
HEXOUT: 'DECODE WORD INTO HEX CHARACTERS AND OUTPUT
'************************************************* ********************
'data to decode is placed in variable SCRATCH
'TX_CHAR is decodes ascii character
JUNK = 3 'number of hex characters
WHILE JUNK < 255
BITS4 = SCRATCH >> (4 * JUNK) 'MOST SIGNIFICANT 4 BITS
BITS4 = BITS4 & $0F
IF BITS4 < 10 THEN
TX_CHAR = BITS4 + $30
GOSUB SNDCHAR: 'INCREMENT BUFFER POINTERS
ELSE
TX_CHAR = (BITS4 - 10) + $41
GOSUB SNDCHAR: 'INCREMENT BUFFER POINTERS
endif
JUNK = JUNK -1
WEND
RETURN
Enjoy...
Art
- 7th July 2017, 16:56
This isn’t tested, but I believe it will split them,
but unsure if you meant ASCII values rather than these real values.
bytea = $2B
byteb = bytea >> 4
bytea = bytea - (byteb << 4)
‘ byteb = $02
’ bytea = $0B
Art
- 7th July 2017, 17:07
If that were to be output as a pair of ASCII bytes, I might go (after the above code)..
IF bytea > 9 THEN
bytea = bytea + $07
ENDIF
bytea = bytea + $30
IF byteb > 9 THEN
byteb = byteb + $07
ENDIF
byteb = byteb + $30
pedja089
- 7th July 2017, 17:39
Or simple use arraywrite:
OutputArray VAR BYTE[6]
ARRAYWRITE OutputArray,[HEX2 byte1, HEX2 byte2,..., HEX2 byte6]
richard
- 8th July 2017, 04:17
Or simple use arraywrite:
OutputArray VAR BYTE[6] ???
ARRAYWRITE OutputArray,[HEX2 byte1, HEX2 byte2,..., HEX2 byte6]
OutputArray VAR BYTE[12]
mpgmike
- 11th July 2017, 15:39
You might try using:
HEX1 VAR WORD
HEX2 VAR WORD
BYTE1 VAR BYTE (etc)
BYTE1 = HEX1.HIGHBYTE
BYTE2 = HEX1.LOWBYTE
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.