binary conversion issues

G

godavemon

I'm using python's struct and binascii modules to write some values
from my parser to binary floats. This works great for all of my binary
files except one. For some reason this file is saving to 836 (stated
by my command shell) bytes instead of 832 like it should. It sounds
like an issue with whatever's writing that particular file out but I've
checked it 100 times. Also python can read in the 836 byte file, find
it has a size of 832 bytes and convert back each value perfectly.
However, when I read in the file in C++ it finds a file of size 836 and
in the middle of the data starts spitting out junk. Has anyone run
into an issue like this or have an idea of what it could be?
 
S

Simon Forman

godavemon said:
I'm using python's struct and binascii modules to write some values
from my parser to binary floats. This works great for all of my binary
files except one. For some reason this file is saving to 836 (stated
by my command shell) bytes instead of 832 like it should. It sounds
like an issue with whatever's writing that particular file out but I've
checked it 100 times. Also python can read in the 836 byte file, find
it has a size of 832 bytes and convert back each value perfectly.
However, when I read in the file in C++ it finds a file of size 836 and
in the middle of the data starts spitting out junk. Has anyone run
into an issue like this or have an idea of what it could be?

Not enough information.

Code and data samples would help.
 
G

Grant Edwards

I'm using python's struct and binascii modules to write some values
from my parser to binary floats. This works great for all of my binary
files except one. For some reason this file is saving to 836 (stated
by my command shell) bytes instead of 832 like it should.

I'm just making a wild-ass guess since you didn't provide any
sample code or data, but it sounds like cr/lf translation
problem to me. Make sure you open the files in binary mode
using the "b" flag.
 
G

godavemon

You guys are awesome! I had to set the 'b' flag when writing the
binaries.

file_obj = open('filename.bin', 'wb')

instead of just using 'w'

It worked fine for all of the other 10 binary files I made, but had a
small error with one of them. In that one file's case it wrote out an
extra 4 bytes in the middle somewhere. Strange.

Thanx for your help.
 
G

Grant Edwards

You guys are awesome! I had to set the 'b' flag when writing the
binaries.

file_obj = open('filename.bin', 'wb')

instead of just using 'w'

It worked fine for all of the other 10 binary files I made,
but had a small error with one of them. In that one file's
case it wrote out an extra 4 bytes in the middle somewhere.
Strange.

If you leave off the 'b' (and you're running on windows), any
byte written to the output file that has a value of 0x0A will
get converted to the two byte sequence 0x0A 0x0D. The extra
bytes in the resulting file are the 0x0D bytes that were
inserted after any 0x0A bytes in the written data.

If the data you write doesn't have any 0x0A bytes, then nothing
gets changed, and your file contains what you expect. If the
data does does have 0x0A bytes, you get an extra 0x0D inserted
after each one.

(It wasn't _that_ wild-ass a guess, since a lot of people get
tripped up by the line-ending conversion issue.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top