473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary files, substrings and (un)packing.

Hello there,

I'm trying to process a binary file and I really don't know how. The
story: gPhoto2 downloads the images from my camera just fine, but
small areas of 10x3 pixels are screwed up. I found exactly where the
problem happens and wrote a simple interpolation filter that works
like a charm... under DOS, using the old QuickBasic. I've used Perl
for a while, but never to handle binary data, so I'm at a loss; and I
really want to get this working under Linux, so I can stop switching
OSs every time I download photos from my camera.

Basically, I need to extract two substrings from a binary string,
average them and then reinsert them in the string. I gather I must use
unpack() with the substrings to return two integer arrays with the
byte values, create a third array with the averages and then use
pack() to convert them to binary again and then reinsert them into the
bigger string. Problem is, I have no clue how to do it.

I'll put an example and see if someone can help me do this: let's say
the input buffer is '00012188856500 0' (those are the ascii values
represented by numbers, not the actual numbers in ascii form). I want
to use unpack to get the substring '121' (location 4, 3 bytes long) in
an array {1,2,1} and the substring '565' (location 10, 3 bytes long)
in an array {5,6,5}, then average the two arrays to get a {3,4,3}
array, then use pack to reinsert them in the buffer so it contains
'00012134356500 0'. How on earth can I do that?

Thanks in advance,
Leandro.

--
"A mind stretched by a new idea never goes back to its original
dimensions."
My Junk Box: http://LeandroTLZ.DeviantArt.com/?complangperl
Jul 19 '05 #1
1 11428
In article <30************ *************@p osting.google.c om>, Leandro
Pardini <no****@leandro pardini.com.ar> wrote:
Hello there,

I'm trying to process a binary file and I really don't know how. The
story: gPhoto2 downloads the images from my camera just fine, but
small areas of 10x3 pixels are screwed up. I found exactly where the
problem happens and wrote a simple interpolation filter that works
like a charm... under DOS, using the old QuickBasic. I've used Perl
for a while, but never to handle binary data, so I'm at a loss; and I
really want to get this working under Linux, so I can stop switching
OSs every time I download photos from my camera.

Basically, I need to extract two substrings from a binary string,
average them and then reinsert them in the string. I gather I must use
unpack() with the substrings to return two integer arrays with the
byte values, create a third array with the averages and then use
pack() to convert them to binary again and then reinsert them into the
bigger string. Problem is, I have no clue how to do it.

I'll put an example and see if someone can help me do this: let's say
the input buffer is '00012188856500 0' (those are the ascii values
represented by numbers, not the actual numbers in ascii form). I want
to use unpack to get the substring '121' (location 4, 3 bytes long) in
an array {1,2,1} and the substring '565' (location 10, 3 bytes long)
in an array {5,6,5}, then average the two arrays to get a {3,4,3}
array, then use pack to reinsert them in the buffer so it contains
'00012134356500 0'. How on earth can I do that?

Thanks in advance,
Leandro.


I am not sure from your description what exactly is the input of your
data. You show 15 digits from 0 - 8, so I will assume that they are
4-bit integers packed into a 60-bit binary string. Here is a way to do
what you want (I first use pack to generate the 60-bit string, then use
unpack to get the 15 4-bit nybbles, do the averaging and replacing,
then use pack again to generate the 60-bit result:
#!/opt/perl/bin/perl

use strict;
use warnings;

# create binary value for testing
my $input = pack("h" x 15, split(//,'0001218885650 00'));

# unpack bytes in input
my @bytes = unpack("h" x 15, $input );
print "input: ", @bytes, "\n";

# average and replace
for my $i ( 0..2 ) {
$bytes[6+$i] = int(($bytes[3+$i] + $bytes[9+$i])/2);
}

# pack into binary string
my $output = pack("h" x 15, @bytes);

# unpack and print result
my @result = unpack("h" x 15, $output);
print "result: ", @result, "\n";

FYI: this newsgroup is defunct. Try comp.lang.perl. misc in the future
for better responses.
Jul 19 '05 #2

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

Similar topics

1
2296
by: Johannes | last post by:
Hi, I tried to pack eight integer values and one string into one binary string, which I would like to store in a mysql db. I encountered two problems doing this: 1) $this->packed = pack('N8A*', $this->value1, $this->value2, $this->value3, $this->value4, $this->value5, $this->value6, $this->value7, $this->value8, $this->stringvalue);
0
3878
by: Jeremy Robbins | last post by:
I am trying to complete some operations on a binary file and was hoping someone could help speed things up a little for me. I can get everything to work no problem I would just like to speed it up some. I have rather large files with a bunch of data points. Each data point represents a different parameter at a different time, I want to skip...
6
23568
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
18
2025
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is...
1
1643
by: QbProg | last post by:
Hello, what I'm saying here is about VS 2005 WITHOUT service pack. I have two DLL projects, with EXACTLY the same project preferences and settings. Both have alignment set to "default", it should be 8. Surpise number one: In project B I include a file of project A in this way #include <FILE_FROM_A.h>
10
7474
by: =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post by:
I have a binary file created using C++ that contains an object of the structure: struct student { int roll_no; char name; char qualification; };
4
1318
by: James Arnold | last post by:
I have a number of different files which I would like to combine into one file, which can later be separated into the individual files again. In essence, a zip archive, only without the compression. I do not wish to use any 3rd party components. Are there any built-in methods for achieving this? (The VB6 property bag did something...
9
3633
by: Jonathan Fine | last post by:
Hello I want to serialise a dictionary, whose keys and values are ordinary strings (i.e. a sequence of bytes). I can of course use pickle, but it has two big faults for me. 1. It should not be used with untrusted data. 2. I want non-Python programs to be able to read and write these dictionaries.
1
3497
by: frp | last post by:
Hello, I need help with C#. How can I create from few files only one encoded file. And then decode one file to few files. Something like packing to ZIP with password. But it is not neccesary use packing into zip. And is there any example? Tahnks.
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
720
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.