View Full Version : Hexidecimal & Decimal conversions in PHP problems...


Stormx
10-08-2005, 05:23 PM
I have an applet that requires colours in a decimal bgr format (Yes, it was made by microsoft ;-))

PHP has a couple of functions for hex to dec and vise-versa; dechex() and hexdec()


The problem comes with hexdec. Say we wanted to convert 0000FF to decimal. it comes out as 255. Why? Because the 0's are just padding

Its like 005 is the same as 5 in decimal.

I have a couple of ideas on how to work round this:
Would it be possible to add a digit to the front of the string, and then substract it from the resulting decimal?

e.g.
1000FF
Then take away the decimal equivilent of 100000

Or...

Would it be possible to convert each section (2 digits of hex) seperately, the mould them back together as a decimal?

Stormx
10-08-2005, 06:03 PM
Meh

Actually, forget it. Looks like all i need to do is zerofill when converting dec to hex.