Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to read byte

Posted on 2006-11-04
7
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

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 100 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
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…
Suggested Courses

618 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