monkey_05_06 Posted December 13, 2006 Share Posted December 13, 2006 I've already been looking at sprintf and str_pad but the problem is that sprintf strips any leading zeroes (i.e., 0x00FF00 becomes "FF00") and if I just use str_pad to pad it back out to 6 digits it messes up the hex value (i.e., "FF0000" or "0FF000" instead of "00FF00"). So is there a way to get the number of leading zeroes or to format a string [b]with[/b] the leading zeroes?I found a function on php.net to convert hex values into RGB values and I'm trying to modify it so it will accept raw hex input instead of only pre-formatted strings (i.e., accepting 0x00FF00 in addition to the strings "0x00FF00", "00FF00", and "#00FF00").Thanks for any help with this issue!monkey_05_06 Link to comment https://forums.phpfreaks.com/topic/30554-formatting-hexdecimal-into-6-digit-string-solved/ Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 The [url=http://www.php.net/sprintf]sprinft()[/url] is fine for this:[code]<?php$hex = 0xFF00;echo sprintf("%06X",$hex);?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/30554-formatting-hexdecimal-into-6-digit-string-solved/#findComment-140659 Share on other sites More sharing options...
monkey_05_06 Posted December 13, 2006 Author Share Posted December 13, 2006 If you use sprintf like that then it will make the string into "00FF00" when you enter 0xFF00. Correct me if I'm wrong in my thinking, but shouldn't 0xFF00 be the same as 0xFF0000, i.e., "FF0000" not "00FF00"?[b][EDIT:][/b]Nevermind...this is hexadecimals not binary numbers. Link to comment https://forums.phpfreaks.com/topic/30554-formatting-hexdecimal-into-6-digit-string-solved/#findComment-140664 Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 You're wrong in your thinking.Why should using HEX be any different from using DecimalsIf you have $x = 100;you don't expect it to be the same as 100000 when formated to 6 characters (padded with zeros), you would expect it to become 000100.Ken Link to comment https://forums.phpfreaks.com/topic/30554-formatting-hexdecimal-into-6-digit-string-solved/#findComment-140668 Share on other sites More sharing options...
monkey_05_06 Posted December 14, 2006 Author Share Posted December 14, 2006 I was thinking of hexadecimals like binary numbers. Basically I figured since I was defining it as 0xFF00 then it would set FF as the first byte since it was the first after the 0x and it would then become padded to the right with zeroes. After considering it I realized that I was just being dumb (notice the edit 2 minutes before your post). ;)Thanks for bearing with me through my brain-farts. :) Link to comment https://forums.phpfreaks.com/topic/30554-formatting-hexdecimal-into-6-digit-string-solved/#findComment-140929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.