Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP to read a file and print the value as Decimal

    Hello,

    I would like a script to read a text file containing a single ASCII character and print it out on a webpage as it's Decimal value. I cannot find such script anywhere and know few of PHP coding to modify the below code.

    Below is a script I found which prints as Hexadecimal.

    <?php

    $handle = @fopen("DATA.TXT", "r");
    if ($handle) {
    while (!feof($handle)) {
    $hex = bin2hex(fread ($handle , 4 ));
    print $hex."\n";
    }
    fclose($handle);

    }

    ?>



    ` DATA.txt contains:
    t

    Any help appreciated. Thanks, Jon.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I assume you don't mean 'decimal' then, since there is no floating point representation for an ascii character. Use the ord() function to pull the hex representation of the character instead. If you want to convert to an integer, you can use base_convert to change the base from 16 to 10.

  • #3
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,383
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    PHP Code:
    $data=file("DATA.TXT");
    echo 
    ascii_to_dec($data[0]); 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by mlseim View Post
    PHP Code:
    $data=file("DATA.TXT");
    echo 
    ascii_to_dec($data[0]); 
    O.o
    There's no ascii_to_dec function in PHP :P

    Edit:
    Oh that does remind me though. I needed to verify the base returned by ord, and its actually base 10, not base 16. So an ord('t') will be 116, not 0x74 (which yes is the exact same thing). Since ord doesn't return a string, no conversion is necessary.
    Last edited by Fou-Lu; 01-16-2012 at 10:19 PM.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fou-Lu, so converting the hex to decimal?...

    mlseim, unfortunately your code resulted in errors

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by jmknash View Post
    Fou-Lu, so converting the hex to decimal?...

    mlseim, unfortunately your code resulted in errors
    Its already an integer. Making a decimal doesn't make any sense since there is no fractions in ascii. If for whatever reason you want it that way you can cast it to a float.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •