Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python - Reading binary file

  • 13-09-2004 5:49pm
    #1
    Registered Users, Registered Users 2 Posts: 763 ✭✭✭


    This has been wrecking my head for the last hour :)

    I'm trying to read in numerical data from a binary file (lets call it blah) using:
    blah.read(3)
    
    The contents of the file:
    ff 41 ff
    
    The string i get back from read:
    '\xffA\xff'   (converts the 0x41 into ascii character A)
    
    What i want:
    '\xff\x41\xff'
    

    So is there any way of preventing python from converting bytes to ascii in the output string?


Comments

  • Registered Users, Registered Users 2 Posts: 568 ✭✭✭phil


    Data from a file is always a String. You need to look at the ``struct'' module and at the pack/unpack functions.

    Regards,

    Phil.


  • Registered Users, Registered Users 2 Posts: 763 ✭✭✭Dar


    Sorted, thanks!


Advertisement