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

• - 2nd February 2019, 06:42
CuriousOne
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?
• - 2nd February 2019, 07:21
sayzer
Re: Converting dec to hex, no built-in support?

You will find everything you would need for DS1302 there.
• - 2nd February 2019, 07:57
CuriousOne
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, 11:10
CuriousOne
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, 12:48
richard
Re: Converting dec to hex, no built-in support?
Quote:

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
• - 2nd February 2019, 15:44
CuriousOne
Re: Converting dec to hex, no built-in support?
great :)
at least it works with DS1302 :)
• - 8th February 2019, 21:10
Amoque
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, 05:03
Art
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, 17:18
CuriousOne
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, 12:23
Charlie
Re: Converting dec to hex, no built-in support?
Quote:

Originally Posted by CuriousOne
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.
• - 11th February 2019, 12:59
pedja089
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, 14:08
Dave
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