CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to modify a part of a binary data?

 



andrei
New User

Sep 19, 2009, 7:30 PM

Post #1 of 3 (702 views)
How to modify a part of a binary data? Can't Post

Hi everybody!

here is a simple question.

Suppose that I have some binary data stored in $data (a scalar variable). For simplicity assume, that the length of $data is 5 bytes which can be interpreted as one byte + one integer packed together. I want to modify the integer in a way that is similar to the function vec:

Code
vec EXPR,OFFSET,BITS

In other words, I do not want to unpack the data into an array of a byte and an integer, modify the integer, then pack it again. I would rather do it directly if it was possible. Any suggestions?


shawnhcorey
Enthusiast


Sep 20, 2009, 3:42 AM

Post #2 of 3 (694 views)
Re: [andrei] How to modify a part of a binary data? [In reply to] Can't Post


In Reply To
In other words, I do not want to unpack the data into an array of a byte and an integer, modify the integer, then pack it again. I would rather do it directly if it was possible. Any suggestions?


KISS. Code that is difficult to understand is costly to maintain.

In other words, unpack it, modify it, pack it.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


alager
Novice

Oct 8, 2009, 1:57 PM

Post #3 of 3 (590 views)
Re: [andrei] How to modify a part of a binary data? [In reply to] Can't Post

You don't have to put it into actual temporary variables, if that is what you are trying to avoid.

Here is an example of a 16bit binary value that is incremented.

Code
 $key = pack "n", ((unpack "(n)*", $key)+1);

But you still have to pack and unpack...


Aaron

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives