如何将以下java代码转换为Perl? [英] How can I convert below java code to Perl?

查看:136
本文介绍了如何将以下java代码转换为Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am in need of below Java code converted to PERL script. Can anyone help me in this context?




<pre> public static String hex(byte[] bytes) {
    char[] c = new char[bytes.length * 2];
    for (int i = 0; i < bytes.length; i++) {
        int j = (bytes.length - i - 1) * 2;
        c[(j + 0)] = HEX[(bytes[i] >> 4 & 0xF)];
        c[(j + 1)] = HEX[(bytes[i] >> 0 & 0xF)];
    }
    return new String(c);
}





我的尝试:



i不知道PERL。请在这方面帮助我。



What I have tried:

i have no idea of PERL.Please help me in this regard.

推荐答案

第一步是了解代码在做什么:

代码似乎将一个字节数组转换为一个十六进制字符串,其中最低字节位于字符串的右侧(它取决于 HEX的定义数组,但我想它只是字符0到9和a到f的映射。



然后你可以在网上搜索一些东西比如perl hex array to string。这样,你会找到像 perl这样的解决方案。整数数组到一个HEX字符串中 - Stack Overflow [ ^ ]。



从我的观点来看,你应该使用一个强大的Perl转换函数: unpack - perldoc.perl.org [ ^ ]。如果输入参数是一个数组,请参阅上面的SO链接。



如果它是包含二进制数据的任何变量,则更简单:

The first step is to understand what the code is doing:
The code seems to convert an array of bytes to a string of hex characters where the lowest byte is on the right side of the string (it depends on the definition of the HEX array but I guess it is just a mapping to the characters 0 to 9 and a to f).

You can then search the web for something like "perl hex array to string". Doing so, you will find solutions like perl - Convert an array of integer into a string of HEX - Stack Overflow[^].

From my point of view you should use one of the powerful Perl conversion functions: unpack - perldoc.perl.org[^]. If the input argument is an array, see the above SO link.

If it is any variable containing binary data, it is much simpler:
my( 


hexString )= unpack(' H *'
hexString ) = unpack( 'H*',


bytes );
bytes );



示例:


Example:

my 


这篇关于如何将以下java代码转换为Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆