473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Binary data handling ?

Hello ppl,

I'm having difficulties to accomplish some simple chores with binary data.
I'm having a string (?) which I received via an HTTP transactions which is a
binary file.
Problem is the webserver I'm communicating with injected a \x0D before every
\x0A,
and I need to remove those 0x0D characters from my buffer before saving it
to disk.

any ideas ?

I tried the following without any success:
string.replace("%c%c" % (13,10), "%c" % (10))
string.replace("\x0d\x0a", "\0x0a")

thx
~B
Jul 18 '05 #1
5 7526
Bill Loren wrote:

Hello ppl,

I'm having difficulties to accomplish some simple chores with binary data.
I'm having a string (?) which I received via an HTTP transactions which is a
binary file.
Problem is the webserver I'm communicating with injected a \x0D before every
\x0A,
and I need to remove those 0x0D characters from my buffer before saving it
to disk.
This sounds wrong. I don't think a properly configured web server should
be transmitting unencoded binary files with newline conversion.
any ideas ?

I tried the following without any success:
string.replace("%c%c" % (13,10), "%c" % (10))
string.replace("\x0d\x0a", "\0x0a")


Strings are immutable. Are you expecting the above to change the string
(which doesn't happen) or to return a new string with the changes made?
Assign the result of the replace() call to a new variable and it should
work. (Except in the latter example you should have \x0a, not \0x0a.)

-Peter
Jul 18 '05 #2
"Bill Loren" <lo*****@bezeqint.net> wrote in message news:<ma**********************************@python. org>...
Hello ppl,

I'm having difficulties to accomplish some simple chores with binary data.
I'm having a string (?) which I received via an HTTP transactions which is a
binary file.
Problem is the webserver I'm communicating with injected a \x0D before every
\x0A,
and I need to remove those 0x0D characters from my buffer before saving it
to disk.

any ideas ?

I tried the following without any success:
string.replace("%c%c" % (13,10), "%c" % (10))
string.replace("\x0d\x0a", "\0x0a")

thx
~B

# A short function to generate a string with 0x0D's
def make_bin(): .... bytes=[]
.... for i in range(20):
.... bytes.append(chr(i))
.... # every 5th char is a 0x0D
.... if not i%5:
.... bytes.append(chr(0x0D))
.... return ''.join(bytes)
.... binary=make_bin()
binary '\x00\r\x01\x02\x03\x04\x05\r\x06\x07\x08\t\n\r\x0 b\x0c\r\x0e\x0f\r\x10\x11\x12\x13' # split the string at the 0x0D's and join it again
binary_without_x0D=''.join(binary.split(chr(0x0D)) )
#et voila
binary_without_x0D '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\ x0e\x0f\x10\x11\x12\x13'


Regards
Peter
Jul 18 '05 #3
Bill Loren wrote:

about the code problem, I did fix that bug you mentioned, but it still
doesn't work.
the code is:
data = data.replace(...the two options I mentioned before...)
but alas... no replacement...


Since the following clearly works, you must be confused about what
is in the string called "data" prior to the replacement:

C:\>python22
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
s = 'test 1\r\ntest 2\rtest 3\ntest4\r\n\r\ntest5'
s 'test 1\r\ntest 2\rtest 3\ntest4\r\n\r\ntest5' t = s.replace('\r\n', '\n')
u = s.replace('\x0d\x0a', '\x0a')
v = s.replace('%c%c' % (13, 10), '%c' % (10))
t 'test 1\ntest 2\rtest 3\ntest4\n\ntest5' u 'test 1\ntest 2\rtest 3\ntest4\n\ntest5' v

'test 1\ntest 2\rtest 3\ntest4\n\ntest5'

(Please post to the newsgroup/mailing list instead of mailing directly,
so that others can benefit from or participate in the discussion.)

-Peter

Jul 18 '05 #4
"Bill Loren" <lo*****@bezeqint.net> wrote:

I'm having difficulties to accomplish some simple chores with binary data.
I'm having a string (?) which I received via an HTTP transactions which is a
binary file.
Problem is the webserver I'm communicating with injected a \x0D before every
\x0A,
and I need to remove those 0x0D characters from my buffer before saving it
to disk.

any ideas ?


I'll bet you real money that the problem is not in the web server. I'd
wager that the string is correct when you receive it, but that you are
writing it to file like this:
file('out.txt','w').write(download)

On a Windows system, that'll turn all the LFs into CR-LFs. Use this
instead:
file('out.txt','wb').write(download)
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #5
Indeed !

thanks !!!

if I use the 'wb' will it work on a unix system, too ?

~B
----- Original Message -----
From: "Tim Roberts" <ti**@probo.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Saturday, August 30, 2003 7:11 AM
Subject: Re: Binary data handling ?

"Bill Loren" <lo*****@bezeqint.net> wrote:

I'm having difficulties to accomplish some simple chores with binary data.I'm having a string (?) which I received via an HTTP transactions which is abinary file.
Problem is the webserver I'm communicating with injected a \x0D before every\x0A,
and I need to remove those 0x0D characters from my buffer before saving itto disk.

any ideas ?


I'll bet you real money that the problem is not in the web server. I'd
wager that the string is correct when you receive it, but that you are
writing it to file like this:
file('out.txt','w').write(download)

On a Windows system, that'll turn all the LFs into CR-LFs. Use this
instead:
file('out.txt','wb').write(download)
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David List | last post by:
I am wondering what I miss to be able to handle binary data from the mysql client. I have ensured that the user has file_priv set to 'Y' and that max_allowed_packet is larger that the binary lumps...
8
by: Bruce Lee | last post by:
Hi I have the following 2 questions: 1.How to write binary data to a text file in C? I have a number like 10001010110001 say, I need to put it in the text file so that I can see the corresponding...
2
by: Feng | last post by:
Hi, I need a VB.Net function that reads in a stream of binary data coming in from a legacy data source. The data are actually hex numbers in binary format but the problem is that I don't know...
4
by: Holger Marzen | last post by:
Hi all, AFAIK it is possible for columns to be very large, up to about 2 GB. Are there any hints or experiences about storing binary data (jpg-images, pdf-documents) in PostgrreSQL with or...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
0
by: Wescotte | last post by:
I'm abit confused on how to work with binary data with an ODBC connection (My database is DB2 btw) Say I have a table like CREATE TABLE EJWLIB.BLOBTEST ( ID NUMERIC(5) NOT NULL, FILENAME...
3
by: stockblaster | last post by:
Hello all.. Is it possible to convert a DataTable (i create the DataTable from a CSV file) into binary data and save it into an sql 2005 table (into binary field). After that I want to...
0
by: perlprod | last post by:
Hi, I want to generate a data file that is to be read by a 'C' program. But I got in dilemma after checking the file generated by the following program. $buffer = ""; $path = "C:/Documents...
9
by: lokeshrajoria | last post by:
hello everybody, i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.