Jump to content

[SOLVED] decimal to hex converting


bbunlock

Recommended Posts

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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.