A less elegant but (perhaps) faster way to pad is with substr with a negative length argument. I use it in this tiny function which formats computed rgb color codes for style sheets:<?function toColor($n){return("#".substr("000000".dechex($n),-6));}?>

I like the example with the bitwise operations but if the value of color[0] is less than 16 it's not accurate:example:color[0]: 0;color[1]: 0;color[2]: 255; function hexColor($color) { return dechex(($color[0]<<16)|($color[1]<<8)|$color[2]);}It returns "ff", which is not legit RGB color...so my solution is to combine the function above with: function toColor($n){return("#".substr("000000".dechex($n),-6));}

If you gotta deal with array of rgb values this is my solution:------------------------------------------------------function hexColor($color) { $rgb = dechex(($color[0]<<16)|($color[1]<<8)|$color[2]); return("#".substr("000000".$rgb, -6));}------------------------------------------------------

I figured out another way to do this:if you have a very long decimal number in gmp format (you can always create a gmp number with gmp_init($numberstring), you can simply do gmp_strval($gmpnumber, 16), where $gmpnumber is your gmp number, and the 16 is the base you want to transform it to. Worked for me like a charm, also works for other bases.

I wrote this to convert hex into signed int, hope this helps someone out there... peace :)

<?php
$hex = dechex(-32767);
$dec = shexdex($hex);

function shexdex($hex){
$dec = hexdec($hex);

// Negative number (in binary if the msb is 1 then it is neg)
// since one hex is 4bits, the value 8 and up contain msb of 1, hence negative number
if ($hex[0] >= '8'){ // Note: if you use the digit 8 it is not the same as '8'
$dec -= 1;
$dec = ~$dec;

These are functions to convert roman numbers (e.g. MXC) into dec and vice versa.Note: romdec() does not check whether a string is really roman or not. To force a user-input into a real roman number use decrom(romdec($input)). This will turn XXXX into XL for example.

I was challenged by a problem with large number calculations and conversion to hex within php. The calculation exceeded unsigned integer and even float range. You can easily change it for your needs but it is, thanks to bcmath, capable of handling big numbers via string. This function will convert them to hex.

In this specific example though, since I use it for game internals that can only handle 32 bit numbers, it will truncate calculations at 8 digits. If the input is 1 for example it will be filled up with zeros. Output 00000001h.

Of course I don't claim it to be a good one, but it works for me and my purpose. Suggestions on faster code welcome!

If you want your code to be portable to amd64 or xeons (which are now quite popular with hosting companies) then you must ensure that your code copes with the different length of the result for negative numbers (and the max value, although that is probably less critical).

I see a lot of less-than-optimal functions posted on this page, so I feel I have to give some better examples... due to the sheer size of this collection, I have made it available on my server, rather than copy/paste it into these comments.

now, here is a nice and small function to convert integers to hex strings and it avoids use of the DECHEX funtion because that function changed it's behavior too often in the past (now, in PHP version 4.3.2 it works with numbers bigger than 0x7FFFFFFF correctly, but i need to be backward compatible).

this function should be not too slow and is really simple.I don't know, if the DECHEX function in the future will pad it's output to ever be 8 characters in length - so for backward compatibility reasons even in future PHP versions i avoided to use it.