Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I am trying to pack bytes into as few unicode characters as possible. Consider the Twitter 140 character limit, which allows byte-heavy unicode characters. I believe I can squeeze 3 8-bit ascii characters into a single 20-bit unicode character, which I am currently trying via the following:

$\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H5)*'; print; $_=pack'(H5)*',map{sprintf"%05X",$_}unpack'(U)*'; print;
Hello World!
He`lo Wopld

Note that when I attempt to pack'(H5)*' the same data I map/unpacked, (almost*) every third character is garbled, a symptom of dealing with an odd 5 hex characters, I'm sure. What is the appropriate way to do this without losing bits?

Packing/Unpacking with the normal 16-bits works as expected:

$\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H4)*'; print; $_=pack'(H4)*',map{sprintf"%04X",$_}unpack'(U)*'; print;
Hello World!
Hello World!

...and even allows 'n*' packing, since n is a 16-bit unsigned short:

$\="\n"; $_='Hello World!'; print; $_=pack'(U)*',map{hex}unpack'(H4)*'; print; $_=pack'n*',unpack'(U)*'; print
Hello World!
Hello World!
Thanks for the help, and Cheers!

In reply to Losing Bits with Pack/Unpack by o0lit3

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2022-01-20 00:02 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.