+ Reply to Thread
Results 1 to 12 of 12
-
- 2nd February 2019, 07:42 #1
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
Why statements like RTCMIN=HEX TIMEVAR or RTCMIN=$TIMEVAR do not work?
-
- 2nd February 2019, 08:21 #2
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
-
- 2nd February 2019, 08:57 #3
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...
-
- 2nd February 2019, 12:10 #4
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
-
- 2nd February 2019, 13:48 #5
Re: Converting dec to hex, no built-in support?
The code below was borrowed from DS1302 thread and converts decimal to hex.
the code actually converts a binary value between 0 and 99 to packed bcd , its not hexThis is more entertaining than Free to Air TV
-
- 2nd February 2019, 16:44 #6
Re: Converting dec to hex, no built-in support?
great
at least it works with DS1302
-
- 8th February 2019, 22:10 #7
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.
-
- 9th February 2019, 06:03 #8
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.
-
- 10th February 2019, 18:18 #9
Re: Converting dec to hex, no built-in support?
The fact is that there's need, and there's no support, right?
-
- 11th February 2019, 13:23 #10
Re: Converting dec to hex, no built-in support?
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.
-
- 11th February 2019, 13:59 #11
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
-
- 11th February 2019, 15:08 #12
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
RETURNDave Purola,
N8NTA
EN82fn
Similar Threads
-
hex dec ascii
By l_gaminde in forum mel PIC BASIC ProReplies: 6Last Post: - 11th February 2015, 22:11 -
HSERIN/OUT Dec to HEX convertion problem..
By N6VMO_ in forum mel PIC BASIC ProReplies: 3Last Post: - 26th July 2010, 21:48 -
Converting Dec word to Hex string
By tcbcats in forum mel PIC BASIC ProReplies: 1Last Post: - 28th August 2007, 05:44 -
Convert dec/binary to Hex
By SagemFree in forum mel PIC BASIC ProReplies: 2Last Post: - 11th May 2005, 18:00 -
Converting Bytes from hex to Dec
By Rob Martin in forum mel PIC BASIC ProReplies: 7Last Post: - 8th April 2005, 19:44
Bookmarks