Perl's pack function equivalent in Java

后端 未结 2 1120
清歌不尽
清歌不尽 2021-01-23 05:10

I have some Perl code that I need to transpose in Java. In this code I have to deal with Perl\'s pack. Is there an equivalent function in Java? The Perl code looks something lik

2条回答
  •  青春惊慌失措
    2021-01-23 05:56

    Perl's pack / unpack functions are a highly versatile conversion utility with its own format syntax (used in H* here, which makes it take an arbitrarily long hex string as input) of which there is no direct equivalent in the Java world. However, to translate...

    $somevar = pack "H*", $vartoconvert;
    

    ...to Java, you can for example use:

    byte[] somevar = javax.xml.bind.DatatypeConverter.parseHexBinary(vartoconvert);
    

    For more information read the DatatypeConverter class reference from Javadocs.

提交回复
热议问题