0

What base type returned by the function "fread()" of PHP, and how can I convert this type to be shown as hexa on the screen?

If I do it that way:

while(!feof($resource))
{
    $contents = fread($resource, 1);
    $new = intval($contents);
    echo base_convert($new, 10, 16);
}

Its just prints a bunch of zeros on the screen... Why its not the same as doing

$contents = fread($resource, filesize($file_text));
echo bin2hex($contents);

Which prints the normal hexa?

wrg wfg
  • 77
  • 6
  • [fread()](http://php.net/manual/en/function.fread.php) returns a String – RiggsFolly Feb 10 '17 at 13:47
  • @RiggsFolly Can you see the question I edited? – wrg wfg Feb 10 '17 at 14:17
  • `intval()` converts types to an `int` using integer casting rules. It does not find the integer value of a char. Meanwhile, `bin2hex()` takes binary data (ie characters) and converts it to its hex representation. See [this example](https://ideone.com/gFeiP6). Assuming your text file contains something other than numbers, `bin2hex()` is the way to go. – Bytewave Feb 10 '17 at 15:23

1 Answers1

1

It's "not the same" because intval does not do what you think it does. It takes a string and interprets it as an integer. If the string has letters and such in it, it's not a valid integer, and so intval returns 0.

That function is for converting e.g. "1234567"(string) to 1234567(int).

alzee
  • 1,393
  • 1
  • 10
  • 21