Menu

binary to hex output

Help
Ryan
2016-03-07
2016-03-08
  • Ryan

    Ryan - 2016-03-07

    does anyone have a routine to take say the binary value from an A/D pin and send it out over serial in hex, but as a decimal equivilent like the binary '00000001' would go out over serial as hex'31' ?

     
  • lhatch

    lhatch - 2016-03-08

    Send it out in ASCII hex right?

    You need to break it into bytes of tens and ones (base 16 though), then serial
    print with the hex function.

    http://gcbasic.sourceforge.net/help/_hex.html

     

    Last edit: lhatch 2016-03-08
  • mmotte

    mmotte - 2016-03-08

    Look in the GCBasic Help under String Manipulations > Hex.

    There is an example.

     
  • William Roth

    William Roth - 2016-03-08

    First for some clarity. All serial data is binary. There is no difference in what goes out the serial port uesing any of the three lines below.

    Sersend 1, 65
    Sersend 1, 0x41
    Sersend 1, 'b01000001'
    In al three cases the serial out data is the exact same series of 1' s and 0's

    If the question is referring to reading an A/D pin, then sending serial data so that it displays on a terminal in hex format here's how I do it using a subroutine that converts a decimal byte value
    to display as hex.

    ''' Test HEX Display
    ''' William Roth
    ''' 08.02.2016
    '''**************************
    
    ; Connect 10K Pot to pin 36 (AN9)
    
    #chip 16F1939, 16
    #config OSC = INTOSC
    #Config MCLRE = ON
    
     'Using SWI2C for LCD Display
    #define I2C_MODE Master
    #define I2C_DATA PORTB.4
    #define I2C_CLOCK PORTB.5
    #define I2C_DISABLE_INTERRUPTS ON
    #define LCD_IO 10
    #define LCD_I2C_Address_1 0x4E
    CLS
    WAIT 1 s
    
    dim ad_val as byte
    
    #define SendAHigh Set PORTB.0 ON
    #define SendALow Set PORTB.0 OFF
    
    '// Sending data to real RS232 Port using Terminal App
    '// If using USB/TTL Converter use "normal" instead of "invert"
    InitSer 1, r9600, 1+WaitForStart, 8, 1, none, invert
    
    'MAIN LOOP
    Do
         ad_val = Readad(AN9)
         dectohex (ad_val)  '// Call sub
    
         '// Display on LCD as Hex
         Locate 0,0
         Print "0x"
         Print CHR(Chr1)
         Print CHR(Chr2)
    
         '// Display on terminal as hex
         SerPrint 1, "0x"
         Sersend 1, Chr1
         Sersend 1, Chr2
         Sersend 1, 13
         Sersend 1, 10
        wait 200 ms
    Loop
    
    sub dectohex (databyte_tmp)
    'Converts Decimal to hex format for display
       chr1 = ((databyte_tmp / 16) + 48)
       if chr1 > 57 then
         chr1 = chr1 + 7
       end If
    
       chr2 = ((databyte_tmp and 15) + 48)
       if chr2 > 57 then
         chr2 = chr2 + 7
       end If
    end sub
    END
    
     
    • Ryan

      Ryan - 2016-03-08

      almost, but i need the hex to be the equivilent of the decimal, say the a/d reads 32 then i need the serial output to be '0x33' '0x32' so i get a "32" rather than '0x20'

       
      • Chris Roper

        Chris Roper - 2016-03-08

        I think it is a terminology issue, you are asking for the ASCII code not
        HEX code.
        There are several functions in BASIC to format numbers for printing,
        unfortunately I don't have access to the GCB documentation at the moment
        but look for a command like ASC().

        Cheers
        Chris

        On Tue, Mar 8, 2016 at 8:59 PM, Ryan rmystique@users.sf.net wrote:

        almost, but i need the hex to be the equivilent of the decimal, say the
        a/d reads 32 then i need the serial output to be '0x33' '0x32' so i get a
        "32" rather than '0x20'


        binary to hex output
        https://sourceforge.net/p/gcbasic/discussion/579126/thread/6492080d/?limit=25#b7d5/e5fb


        Sent from sourceforge.net because you indicated interest in
        https://sourceforge.net/p/gcbasic/discussion/579126/

        To unsubscribe from further messages, please visit
        https://sourceforge.net/auth/subscriptions/

         
      • William Roth

        William Roth - 2016-03-08

        Just use SerPrint 1, (variable) to display the numerical 32 as "32"

        This really has nothing to do with "hex"

        Example:

        Do
        Variable = ReadAD(9)
        SerPrint 1, Variable
        Sersend 1,13
        Sersend 1,10
        wait 500 ms
        Loop

         

Log in to post a comment.