Solved

how to read byte

Posted on 2006-11-04
7
366 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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…

792 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