otuatail Posted September 27, 2008 Share Posted September 27, 2008 hi. Can anyone tell me if it is possible to convert a number to its hex equivelent. I want to generate a random number 0 - 65535 and to convert this to 0000 to ffff Any ideas on this please. Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/126043-solved-convert-decimal-to-hex/ Share on other sites More sharing options...
ratcateme Posted September 27, 2008 Share Posted September 27, 2008 dechex() should do the trick Scott. Quote Link to comment https://forums.phpfreaks.com/topic/126043-solved-convert-decimal-to-hex/#findComment-651799 Share on other sites More sharing options...
Barand Posted September 27, 2008 Share Posted September 27, 2008 for more control over the output (width, leading zeros, case) you can use sprintf() or printf() printf ('%04X', rand(1,65535)); Quote Link to comment https://forums.phpfreaks.com/topic/126043-solved-convert-decimal-to-hex/#findComment-651811 Share on other sites More sharing options...
otuatail Posted September 27, 2008 Author Share Posted September 27, 2008 Thanks guys that worked. Remeber printf from old days. I wanted it in a string and remembered sprintf. Works a treat Thanks. Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/126043-solved-convert-decimal-to-hex/#findComment-651829 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.