On 11/10/2015 02:29 PM, kent nyberg wrote: > On Mon, Nov 09, 2015 at 10:20:25PM -0800, Larry Hudson via Python-list wrote: >> Your questions are somewhat difficult to answer because you misunderstand >> binary. The key is that EVERYTHING in a computer is binary. There are NO >> EXCEPTIONS, it's all binary ALL the time. The difference comes about in how >> this binary data is displayed and manipulated. I want to emphasize, ALL the >> DATA is binary. >> > > Thanks alot for taking the time. > I get it now. I sort of, but not fully, misunderstood the conecpt of binary > files. > The thing I was after; and the thing Im playing with now after a more > succesfull time with google, > is writing more specific things to a file than just strings. > English is not my native language so please forgive me, but > I wanted to write specifc 16bit codes, and read them. And later play with > bitwise operations on them. Sort of. > It might not make sense at all, but hey.. it doesnt have to. > Thanks anyway. :)
You're correct; it doesn't make that much sense. If it were me I'd write out my numbers in text format to the file. You can always read them back in and covert them to a number. Just a quick couple of notes on "binary" vs "text". In the old days on Windows, the difference between "binary" and "ascii" when it came to file reading was simply the interpretation of the end-of-line marker. Whenever you wrote out a \n, it got silently converted to two bytes, 0x0d 0x0a. If you were trying to write a jpeg file, for example, this would corrupt things as you well know. In the Unix world, we never worried about such things because the end-of-line marker was simple 0x0a. It was never translated and never was expanded silently. So when it came to how we worked with files, there was no difference between binary and ascii modes as far as the C library open() was concerned. Now with Python 3, we now again do have to think about the distinction between "text" and "binary" when working with files. If we want to open a text file, we have to open it while specifying the expected text encoding, whether that is UTF-8, UCS-2, UTF-16, or some other old and esoteric encoding. That is to say when Python reads from a text file, there is always going to be a decoding process going on where text file bytes are read in, and then converted into unicode characters. When writing a text file out, unicode characters have to be encoded into a series of bytes. If Python knows what encoding we want, then it can do this automatically as we write to the file. In Python 3 opening a file for binary will read in raw bytes and you can manipulate them however you wish. -- https://mail.python.org/mailman/listinfo/python-list