Jump to content

Priting 8 Char Hex


Irap

Recommended Posts

Hi, I have this very simple code...

 

<?php
echo "<code>";
$hFile = fopen("myfile.ini","rt");
while(!feof($hFile))
{
$string = trim(fgets($hFile,);
$c = substr($string,0,1);
if($c!="[" && $c!="\n" && $c!="\r" && $c!="")
{
	$i = crc32($string);
	echo printf("%8X", $i) ."=". $string ."<br/>";
}
}
fclose($hFile);
echo "</code>";
?>

 

However, it's returning results like this:

4C20553D=4NATION
7BE5EF8C=4RIP
EAAD529E=4SPIDER
4EB7AFA2=4WEED
7367626=4_OR_14
83101629=5CROSS

 

When I wanted leading 0's, like this:

4C20553D=4NATION
7BE5EF8C=4RIP
EAAD529E=4SPIDER
4EB7AFA2=4WEED
07367626=4_OR_14
83101629=5CROSS

 

I've tried "%.8X", which I figured would work... but it just turned all the hashes to 0.

 

Also, which other varients of CRC are commonly used? The results I am getting are not what I expected as from another program...

Link to comment
https://forums.phpfreaks.com/topic/223616-priting-8-char-hex/
Share on other sites

To pad with leading zeros, put a zero after the '%' character:

 

echo printf("%08X", $i) ."=". $string ."<br/>";

 

WHY are you echo-ing printf()? printf() prints a string and returns the length of the string. So the way you have it, it would be printing the hex-string, then the length of that string, then the '=' and the $string. You could do that statement as:

 

printf("%08X=%s<br/>", $i, $string);

Link to comment
https://forums.phpfreaks.com/topic/223616-priting-8-char-hex/#findComment-1156036
Share on other sites

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.