473,486 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Binary Files

I made a little PHP thingy where you can search for any string of
numbers in the first 8 million digits of pi.

However, they're currently stored in ASCII, which means it's 2bytes for
a digit, and it's horribly inefficient. I used perl to pack it as 2
digits a byte, basically storing it as hex without A-F.

My problem is, I need a fast way to search the file by the half byte.
Asides from reading all 8 million digits and converting it to hex, how
else can I do it?
Sep 20 '05 #1
4 1461
Just compress the file with the zlib functions. The compression routine
will squeeze out most of the redundancy.

Sep 20 '05 #2
Just compress the file with the zlib functions. The compression routine
will squeeze out most of the redundancy.


OT: Chung, I was about to tell you off for suggesting that an
irrational number like PI could be compressed but... luckily I paused
for a moment, and googled this
<http://thestarman.dan123.com/math/pi/picalcs.htm> that shows better
than 50% compression rates. So that told me.

Back to the OP, where you go from here depends entirely on what your
search algorithm is doing at the moment with the string version of PI.
If it is a brute force dumb search, it will not need much modification
but it will be slow.

You need to pack the search substring first in the same way as your PI
digits. You probably have to do a minimum of two searches: one to look
for odd-aligned substrings and one to look for even-aligned substrings.
Doing a search with a substring starting or ending on an odd-aligned
digit also means managing the "don't care" digits. You might be able to
devise a suitable regexp to handle all of this. Best of luck. Can you
tell me if my birthday is in there (19590318)?

---
Steve

Sep 20 '05 #3
Steve wrote:
Just compress the file with the zlib functions. The compression routine
will squeeze out most of the redundancy.

OT: Chung, I was about to tell you off for suggesting that an
irrational number like PI could be compressed but... luckily I paused
for a moment, and googled this
<http://thestarman.dan123.com/math/pi/picalcs.htm> that shows better
than 50% compression rates. So that told me.

Back to the OP, where you go from here depends entirely on what your
search algorithm is doing at the moment with the string version of PI.
If it is a brute force dumb search, it will not need much modification
but it will be slow.

You need to pack the search substring first in the same way as your PI
digits. You probably have to do a minimum of two searches: one to look
for odd-aligned substrings and one to look for even-aligned substrings.
Doing a search with a substring starting or ending on an odd-aligned
digit also means managing the "don't care" digits. You might be able to
devise a suitable regexp to handle all of this. Best of luck. Can you
tell me if my birthday is in there (19590318)?

---
Steve


I haven't tried it yet, but I was figuring I could maybe unpack the
whole thing or partrs of it at a time into hex and then search it from
there.

BTW: 19590318 isn't in the first 8 million digits.
I guess you could try (the slow one) for yourself.
http://ericjiang.co.nr/pi.php
Sep 20 '05 #4
I haven't tried it yet, but I was figuring I could maybe unpack the
whole thing or partrs of it at a time into hex and then search it from
there.


Maybe you can get away with that but your performance could be
dreadful. You'd have to deal with searching across part boundaries,
sliding windows and all that jazz. But you will have to decide whether
to optimise execution time, or storage cost, or memory use, or
compromise on everything. What is your main constraint? Space on disk,
memory, execution time or complexity of algorithm?

Could be that I'm underestimating the resources you have: on my desktop
even a brute force search (strpos) is reasonably fast against an 8M
string, and preg_match is faster. However I haven't looked at what this
is doing to my resources. Your server might buckle under a heavy load
if searching in pi becomes the Next Big Thing.

---
Steve

Sep 21 '05 #5

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

Similar topics

2
8879
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
3
2799
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
7
4200
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
50
4876
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
5
6427
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
7
6027
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
1881
by: amfr | last post by:
On windows, is there anything special I have to do to read a binary file correctly?
30
4534
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
2
3225
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
6
5235
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
6964
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
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
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...
1
6842
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
7319
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...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.