Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Losing Bits with Pack/Unpack

by Eily (Monsignor)
on Sep 17, 2020 at 07:56 UTC ( #11121868=note: print w/replies, xml ) Need Help??


in reply to Losing Bits with Pack/Unpack

Well others have already explained why what you're trying to do is a bad idea. Maybe if you told us why you're trying to do something like that we might be able to help find a better solution though.

But, for your information, one reason why this just won't work is that pack is always byte aligned. That's why (H5)* changes every third char. H is half a byte/char, so H2 reads a full one, H4 reads two, and H5 reads two and a half. But the next iteration aligns itself on the next char and simply ignores the half byte that was unread. So you're just losing data, not shifting any bits.

Replies are listed 'Best First'.
Re^2: Losing Bits with Pack/Unpack
by o0lit3 (Friar) on Sep 17, 2020 at 11:55 UTC
    Is "unpack" byte aligned also? If I have not lost any data with the unpack'(H5)%' step, I am wondering how one would go about reversing the process.

      Yes unpack is also byte aligned. unpack moves exactly like pack does given the same pattern, except it reads instead of writes. You did lose data with H5, that's why some chars were changed. One way to see that is to try "hehlo" or "hemlo" instead of hello. You'll see that you get the same result, because four bits in the byte are replaced by 0000 when pack was only given 5 hex digits for 3 bytes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121868]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-10-17 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?