Perl decimal to ASCII conversion

妖精的绣舞 提交于 2019-12-23 03:12:23

问题


I am pulling SNMP information from an F5 LTM, and storing this information in a psql database. I need help converting the returned data in decimal format into ASCII characters. Here is an example of the information returned from the SNMP request:

iso.3.6.1.4.1.3375.2.2.10.2.3.1.9.10.102.111.114.119.97.114.100.95.118.115 = Counter64: 0  

In my script, I need to identify the different sections of this information:

my ($prefix, $num, $char-len, $vs) = ($oid =~ /($vsTable)\.(\d+)\.(\d+)\.(.+)/);

This gives me the following:

(prefix= .1.3.6.1.4.1.3375.2.2.10.2.3.1)  
(num= 9 ) 
(char-len= 10 ) 
(vs= 102.111.114.119.97.114.100.95.118.115)

The variable $vs is the Object name in decimal format. I would like to convert this to ASCII characters (which should be "forward_vs"). Does anyone have a suggestion on how to do this?


回答1:


my $new_vs = join("", map { chr($_) } split(/\./,$vs));



回答2:


Given that this is related to interpreting SNMP data, it seems logical to me to use one or more of the SNMP modules available from CPAN. You have to know quite a lot about SNMP to determine when the string you quote stops being the identifier (prefix) and starts to be the value. You have a better chance of getting a general solution with SNMP code than with hand-hacked code.




回答3:


Jonathan Leffler has the right answer, but here are a couple of things to expand your Perl horizons:

use v5.10;
$_ = "102.111.114.119.97.114.100.95.118.115";
say "Version 1: " => eval;
say "Version 2: " => pack "W".(1+y/.//) => /\d+/g;

Executed, that prints:

Version 1: forward_vs
Version 2: forward_vs

Once both are clear to you, you may hit space to continue or q to quit. :)

EDIT: The last one can also be written

pack "WW".y/.//,/\d+/g

But please don't. :)




回答4:


Simple solution:

$ascii .= chr for split /\./, $vs; 



回答5:


pack 'C*', split /\./

For example,

>perl -E"say pack 'C*', split /\./, $ARGV[0]" 102.111.114.119.97.114.100.95.118.115
forward_vs


来源:https://stackoverflow.com/questions/7142036/perl-decimal-to-ascii-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!