Irap Posted January 6, 2011 Share Posted January 6, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/223616-priting-8-char-hex/ Share on other sites More sharing options...
MMDE Posted January 7, 2011 Share Posted January 7, 2011 this fix is probably not the best, but perhaps it works for you... most likely better ways to do it too! for($i=strlen($string);$i<8;$i++){ echo '0'; } echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/223616-priting-8-char-hex/#findComment-1156020 Share on other sites More sharing options...
DavidAM Posted January 7, 2011 Share Posted January 7, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/223616-priting-8-char-hex/#findComment-1156036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.