bbunlock Posted May 9, 2008 Share Posted May 9, 2008 I have a flex app that was made for me that sends the values via the post method, the problem is that the color values are sent in decimal format. I have used the dec2hex function and it works on some of them but it fails on others, for example the post data might send "16763955" which I can convert using the dechex function into its correct hex color code example "00ff00" BUT some of the decimal values are for example "65535" which converts to "33cc" which is no good becuase the correct value is "0033cc" it apears that dechex will not convert these correctly. is there another function that I have missed that will convert them to the correct hex code? I have sent the flex developer an email a couple of days ago but heard nothing back so diceded to try a php convert instead but having problems as described above. if anyone can suggest another method to get the correct values then please reply to let me know thanks in advance wayne Quote Link to comment https://forums.phpfreaks.com/topic/104913-solved-decimal-to-hex-converting/ Share on other sites More sharing options...
Barand Posted May 9, 2008 Share Posted May 9, 2008 <?php $dec = 65535; $hex = sprintf('%d --> %06X', $dec, $dec); echo $hex; // 00FFFF echo '<br>'; $dec = 13260; $hex = sprintf('%d --> %06X', $dec, $dec); echo $hex; // 0033CC Quote Link to comment https://forums.phpfreaks.com/topic/104913-solved-decimal-to-hex-converting/#findComment-536986 Share on other sites More sharing options...
thebadbad Posted May 9, 2008 Share Posted May 9, 2008 You're having this problem, because leading zeros are capped off the returned string of dechex(). The solution is to always use str_pad(): <?php echo str_pad(dechex(65535), 6, '0', STR_PAD_LEFT); // 00ffff ?> Or Barand's solution Quote Link to comment https://forums.phpfreaks.com/topic/104913-solved-decimal-to-hex-converting/#findComment-536991 Share on other sites More sharing options...
bbunlock Posted May 9, 2008 Author Share Posted May 9, 2008 thank you both for the help, its now working great much appriciated wayne Quote Link to comment https://forums.phpfreaks.com/topic/104913-solved-decimal-to-hex-converting/#findComment-537078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.