473,385 Members | 2,269 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,385 software developers and data experts.

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 '000121888565000' (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
'000121343565000'. 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 11419
In article <30*************************@posting.google.com> , Leandro
Pardini <no****@leandropardini.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 '000121888565000' (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
'000121343565000'. 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(//,'000121888565000'));

# 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
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 =...
0
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...
6
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
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....
1
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...
10
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
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...
9
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.