Solved

how to read byte

Posted on 2006-11-04
7
365 Views
Last Modified: 2010-04-16
what functions should I use to read byte by byte from binary files? I looked up and found that the read() functions. but it returns the whole files in binary.  But I like to read byte by byte.  Thanks in advance.

Kelvin
0
Comment
Question by:templeavenue
  • 4
  • 3
7 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 17873774
From the Python Library Reference under 2.3.9 File Objects:
"read( [size])
Read at most size bytes from the file "

Nuf said?

But before you jump, are you clear on what you want do accomplish. Reading a file byte by byte is probably less efficient than reading, say, a line then accessing each byte in the line.

So what is your goal?
0
 

Author Comment

by:templeavenue
ID: 17877961
I want to make comparions byte(possiblly bits) between the byte read from the file and the reference byte(which I don't need to read from any file).  
0
 
LVL 17

Expert Comment

by:ramrom
ID: 17878378
OK. Well I think you have the information you need. Any questions?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:templeavenue
ID: 17878426
oh...i still haven't figure out how to read byte from file.  Can you please explain?
0
 
LVL 17

Accepted Solution

by:
ramrom earned 50 total points
ID: 17878935
f = file("c:/foo.dat", "rb") # your file may have a different name
while 1:
  byte = f.read(1) # reads(per the documentation above) 1 byte
  if not byte: break # we've reached end of file
  # process the byte

0
 
LVL 17

Expert Comment

by:ramrom
ID: 17882427
I'm glad I could help. I am confused by getting a grade of C. Please note the EE guidelines for grading:

    *  A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

    * B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

    * C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Basedd on that I'd have given a grade of A. Please help me understand how you came up with a different result.

I am also confused in that your original question was "what functions should I use to read byte by byte" and I answered that in my first comment.
0
 

Author Comment

by:templeavenue
ID: 17890273
well..I think I agree with you. When I graded that I just think "whatever".  Sorry, based on the questions I asked, I should have graded as A.  I am willing to change the grade, but I don't know how.. Again. sorry...you have been helped... and thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question