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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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