Jump to content

Formatting hexdecimal into 6-digit string. [SOLVED]


monkey_05_06

Recommended Posts

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
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.
You're wrong in your thinking.

Why should using HEX be any different from using Decimals

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.