Page 1 of 1

Conversion dec to hex and hex to dec

Posted: Wed Oct 18, 2017 3:15 pm
by rbytes
Is there a simple way to convert numbers between these two bases, or do I need to write code to do it!

Thanks.

Re: Conversion dec to hex and hex to dec

Posted: Wed Oct 18, 2017 4:08 pm
by Mr. Kibernetik
Can you please give an example of what kind of conversion do you need? Some kind of pseudo code which imitates desired behavior.

Re: Conversion dec to hex and hex to dec

Posted: Wed Oct 18, 2017 4:55 pm
by rbytes
I found that SPL already converts hex to decimal automatically. If I run the line
#.output(ffh) it will print 255. If I run the line g = ffh, g will equal 255

What I want is a function that will convert decimal to hex, so that when my Unicode reader returns the decimal value of a character, I can convert that to a high and low byte for the user. They can then use your hex byte system to reproduce the character.

In other words a decimal value would need to be converted as follows. t = hex(255). The output should then be “ffh”. I assume the hex function would have to return a string value; otherwise if t was treated as a numeric variable, as soon as I tried to read t, its value would be decimal 255.

I found the following website that gives me the formula in C. I could try to translate that into SPL if there is no current function to do it.

http://scanftree.com/programs/c/c-code- ... xadecimal/

Re: Conversion dec to hex and hex to dec

Posted: Wed Oct 18, 2017 8:17 pm
by Mr. Kibernetik
This is done using #.str function:

#.output(#.str(255,"X"))

SPL has very advanced formatting options which are given in Appendix 1.