Byte to Hex


Closed Thread
Results 1 to 8 of 8

Thread: Byte to Hex

  1. #1
    Join Date
    Jul 2003
    Location
    Lancashire
    Posts
    50

    Default Byte to Hex

    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

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    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

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    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...
    Dave Purola,
    N8NTA
    EN82fn

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    This isn’t tested, but I believe it will split them,
    but unsure if you meant ASCII values rather than these real values.

    Code:
    bytea = $2B
    
    byteb = bytea >> 4
    bytea = bytea - (byteb << 4)
    
    ‘ byteb = $02
    ’ bytea = $0B

  5. #5
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    If that were to be output as a pair of ASCII bytes, I might go (after the above code)..
    Code:
    IF bytea > 9 THEN
    bytea = bytea + $07
    ENDIF
    bytea = bytea + $30
    
    IF byteb > 9 THEN
    byteb = byteb + $07
    ENDIF
    byteb = byteb + $30

  6. #6
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    Or simple use arraywrite:
    OutputArray VAR BYTE[6]
    ARRAYWRITE OutputArray,[HEX2 byte1, HEX2 byte2,..., HEX2 byte6]

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    Or simple use arraywrite:
    OutputArray VAR BYTE[6] ???
    ARRAYWRITE OutputArray,[HEX2 byte1, HEX2 byte2,..., HEX2 byte6]
    OutputArray VAR BYTE[12]
    Warning I'm not a teacher

  8. #8
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Byte to Hex

    You might try using:

    HEX1 VAR WORD
    HEX2 VAR WORD

    BYTE1 VAR BYTE (etc)

    BYTE1 = HEX1.HIGHBYTE
    BYTE2 = HEX1.LOWBYTE

Similar Threads

  1. HEX byte to ASCII output...
    By johnmaetta in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th August 2011, 01:31
  2. Increment a Byte
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 21st November 2009, 12:09
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 19:04
  4. Convert 3-byte hex to dec to display on LCD
    By lab310 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 10th June 2005, 20:20
  5. Splitting a byte?
    By Deadeye in forum General
    Replies: 3
    Last Post: - 29th May 2005, 22:58

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts