Forum Moderators: coopster

Message Too Old, No Replies

PHP equivalent of some C formatting

%081x

         

bcolflesh

2:43 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm told that %081X in C means: "hex format 8 chars wide with 0 fill, upper case"

I need to do the same thing to a string in PHP, so far I've got:

$output = strtoupper(bin2hex($my_string));

How do I do "8 chars wide with 0 fill"?

Regards,
Brent

dmorison

2:52 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, bcolflesh

The following PHP code prints out the number held in $x as an 8 character zero padded hexadecimal string..

$x = 1234;

$s = sprintf("%08X",$x);

echo $s;

Is this what you're looking for?

Cheers.

bcolflesh

3:33 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dear Dmorison,
Thanks for the reply - it always returns 00000000 for me, probably because my "$x" is not a number - it is a Blowfish encrypted string, ex:

¾+©Ío)Œ—þùr"÷Æáßn´!¤EñX¯sÆH7

coverting it to hex first also returns 00000000 with your code.

Any ideas?

Regards,
Brent

dmorison

3:42 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Bret,

I'm afraid i'm not really sure what you're trying to do in that case!

Is the thing that is blowfish'ed a number? If so, you will need to de-blowfish it before trying to format it as an 8 character hexadecimal string...

Can you elaborate as to what you're trying to achieve?

grahamstewart

11:11 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what you are looking for is the str_pad() function.

e.g.


$output = strtoupper(bin2hex($my_string));
$output = str_pad( $output, 8 - count($output), '0', STR_PAD_LEFT );

ShawnR

11:26 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I know what you are trying to do... One way is to do a preg_replace on your string, converting each character to hex using a Bin2hex() call in the replacement string part of the preg_replace. Note that %081X in C means hex format 81 chars wide... Also note that you would have the same problem in C. The C code would have needed to convert the string to an integer first.

Shawn