Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Pack uneven length hex

by Eily (Monsignor)
on Nov 23, 2018 at 09:59 UTC ( [id://1226213]=note: print w/replies, xml ) Need Help??


in reply to Pack uneven length hex

If you have a maximum length for your input sprintf may help: sprintf "%016s", EXPR. It will align all your inputs to your max size, although you could still remove the leading \0s. (And I'm assuming you do want a byte string as the output, otherwise hex might help)

requires you to actually have a variable declared
You could use either map or for to alias the value of your EXPR to $_:
print unpack "H*", pack("H*", map { "0" x (length($_) % 2) . $_ } "a12 +"); print unpack "H*", pack("H*", "0" x (length($_) % 2) . $_ ) for "a12";
That's still not pretty (and you could do even worse, with an anonymous sub).

I don't get the issue with your helper function and circular packages though. Can't you just put the fonction alone in a file, in the worst case? Or, if it really comes to it, just redefine it wherever you need, the function is fairly short anyway:

sub zpad { my ($str,) = @_; length($str)%2 ? "0$str" : $str } print unpack 'H*', pack 'H*', zpad "a30"

Replies are listed 'Best First'.
Re^2: Pack uneven length hex
by Anonymous Monk on Nov 24, 2018 at 06:39 UTC
    A faster way to calculate

    length($_) % 2

    is to do this:

    length($_) & 1

    Gives the same result, but it's often a bit faster, because the former forces the processor to perform a division, whereas the second one is a simple bitwise AND. We grab the last bit, and that's all.

      thanks! .oO( all the divisions I have caused... )

Re^2: Pack uneven length hex
by melez (Sexton) on Nov 23, 2018 at 10:22 UTC
    Thank you for the reply. I don't want to set maximum length on a hex string. I work with very big numbers so hex() is out of question, and in my case byte string is actually what I want to get.
    The map/for is quite interesting, however I was hoping there's a clever way to work with parameters in pack() itself, which would change it's behavior to pad left side (I couldn't find anything like that on my own).
    Inserting a tiny function like this seems to be the best solution, however as this is meant to be in a CPAN package I wanted to keep it as clean as possible, and at least two of my files will require this functionality. Why do it even pad numbers on the right side?
      I was hoping there's a clever way to work with parameters in pack() itself, which would change it's behavior to pad left side

      Not sure that it can be classified as "clever", but if you "scalar reverse()" twice, it seems to produce the correct result (for your given example, at least) :
      print scalar reverse unpack "H*", pack("H*", scalar reverse "a12");
      Cheers,
      Rob

      however I was hoping there's a clever way to work with parameters in pack() itself
      As far as I can tell, there isn't one. Though I agree that would have been interesting.

      this is meant to be in a CPAN package I wanted to keep it as clean as possible
      I suppose that's why you don't want to put the function in its own file? Because it kind of belongs in a separate module?

      Why do it even pad numbers on the right side?
      Because adding things on the right of your data should not change how the left part is interpreted, I guess

      I just thought of another way to do what you want: pack 'H*', 'a12' =~ s/^(.(..)*)$/0$1/rxs. Still not as clear as a function though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found