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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    Can I write hex to a binary file?

    I need to write hex to a binary file. But the functions fputs() and fwrite() only seem to be able to write in strings. If I want to write 064 to the file, I want it to show up as @ (1 byte) not "064" (3 bytes). A test page for the hex output is at http://scorpius.spaceports.com/~xfox/s3kc/edit/php.htm. Is there a function to unescape hex values upon write?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    quote from the manual

    Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.
    Did you open the file in binary mode?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    Originally posted by Spookster
    quote from the manual



    Did you open the file in binary mode?
    Yes, I opened it in binary.

    $fr=fopen('test.bin','w+b');

    But good does that do when you can only write strings? If would like to do something like the following:

    fwrite($fr,%1e); // writing hex bytes into the file.
    //(where % means the hex code for a specific byte)

    That should be specific enough. But if a file is opened in binary, how does that differentiate from any other type of opening? I only know how FTP treats binary and ascii different.


  •  

    Posting Permissions

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