+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2013
    Posts
    546

    Default Converting dec to hex, no built-in support?

    I need to convert decimal value to hex, to send it to RTC. Seems like PBP has no built in convertors?

    I have hex values such as rtcmin, rtcsec, rtchour and so on.
    I have a decimal variable which changes on button press. Have to convert it to hex, but can't find proper statement.
    Of course I can do it by hand, like this

    Code:
    IF TIMEVAR=10 THEN RTCMIN=$10
    IF TIMEVAR=11 THEN RTCMIN=$11
    and so on, but just to set minutes, I will need 60 lines of code!

    Why statements like RTCMIN=HEX TIMEVAR or RTCMIN=$TIMEVAR do not work?

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,236

    Default Re: Converting dec to hex, no built-in support?

    http://www.picbasic.co.uk/forum/showthread.php?t=423

    You will find everything you would need for DS1302 there.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Feb 2013
    Posts
    546

    Default Re: Converting dec to hex, no built-in support?

    Well that was just an example. There are many cases when it's need to convert from DEC to HEX...

  4. #4
    Join Date
    Feb 2013
    Posts
    546

    Default Re: Converting dec to hex, no built-in support?

    I'm adding this here, so it can be found using search by someone else when needed.

    The code below was borrowed from DS1302 thread and converts decimal to hex.

    Code:
    'RTC - target variable in hex
    'decx - source variable in dec
    	RTC=decx DIG 1
    	RTC=RTC<<4
    	RTC=RTC+decx DIG 0

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    1,729

    Default Re: Converting dec to hex, no built-in support?

    The code below was borrowed from DS1302 thread and converts decimal to hex.
    hope nobody actually believes this

    the code actually converts a binary value between 0 and 99 to packed bcd , its not hex
    This is more entertaining than Free to Air TV

  6. #6
    Join Date
    Feb 2013
    Posts
    546

    Default Re: Converting dec to hex, no built-in support?

    great
    at least it works with DS1302

  7. #7
    Join Date
    Apr 2011
    Location
    Portland, Oregon
    Posts
    221

    Default Re: Converting dec to hex, no built-in support?

    I'm not sure what you mean. You do not convert to HEX, you represent in hex. Consider: In your clock example the value is "12". You might represent that in 10 based decimal as "12" or, you might represent it in binary as %1100, or you might represent it in hex as $C. The value is not different, only the representation. You notify PicBasic what notation you are using by prepending a designator %, $, or skip the designator for decimal, except for display).

    Suppose you are writing to PORTB and you want to set all pins high. You may write PORTB = 255, or you may write PORTB = %11111111 or, PORTB = $FF -- all the same thing. No matter how you write it in PicBasic, it is stores as 8-bits binary. In fact in most every statement where a unary (one) number is expected, you may represent it as decimal, hex, or binary. In any mathematical calculation I think you may use (or mix) decimal and hex. There are special rules (bitwise) regarding binary, but it is possible to use binary sometimes.

  8. #8
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,060

    Default Re: Converting dec to hex, no built-in support?

    Yes the only time you need to “convert” between hex, dec,bin is to print something to display or send values out of a serial port for a Human to read.

  9. #9
    Join Date
    Feb 2013
    Posts
    546

    Default Re: Converting dec to hex, no built-in support?

    The fact is that there's need, and there's no support, right?

  10. #10

    Default Re: Converting dec to hex, no built-in support?

    Quote Originally Posted by CuriousOne View Post
    The fact is that there's need, and there's no support, right?
    You are completely missing the point, so let me take a crack at it. Numbers always have the same value. The box of screws on my workbench always has the same number of screws in it, whether I choose to write that number down in binary, hex, decimal, octal, base 12, or any other method of expressing it I choose. PBP can show a number in binary, hex, decimal. The issue is your use of the word "convert". There is nothing to convert and no conversion happening. Period. Full stop. So no, there is no need. If you want to output a number in any of those formats, it's easy to do and explained in the manual.

  11. #11
    Join Date
    Sep 2009
    Posts
    776

    Default Re: Converting dec to hex, no built-in support?

    He probably mean that there is no BCD conversion.
    https://en.wikipedia.org/wiki/Binary-coded_decimal

  12. #12
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,199

    Default Re: Converting dec to hex, no built-in support?

    CuriousOne, When there is not a conversion then, write your own. I did:

    '************************************************* ********************
    HEXOUT: 'DECODE WORD INTO HEX CHARACTERS AND OUTPUT
    '************************************************* ********************
    JUNK = DIGITS - 1
    WHILE JUNK < 255
    BITS4 = VALUE >> (4 * JUNK) 'MOST SIGNIFICANT 4 BITS
    BITS4 = BITS4 & $0F
    IF BITS4 < 10 THEN
    TX_BYTES(TX_OUTPUT) = BITS4 + $30
    ELSE
    TX_BYTES(TX_OUTPUT) = (BITS4 - 10) + $41
    endif
    GOSUB SNDCHAR 'INCREMENT BUFFER POINTERS
    JUNK = JUNK -1
    WEND
    RETURN
    Dave Purola,
    N8NTA
    EN82fn

Similar Threads

  1. hex dec ascii
    By l_gaminde in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th February 2015, 21:11
  2. HSERIN/OUT Dec to HEX convertion problem..
    By N6VMO_ in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th July 2010, 21:48
  3. Converting Dec word to Hex string
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th August 2007, 05:44
  4. Convert dec/binary to Hex
    By SagemFree in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th May 2005, 18:00
  5. Converting Bytes from hex to Dec
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 8th April 2005, 19:44

Members who have read this thread : 26

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