Perl decimal to ASCII

I am extracting SNMP information from F5 LTM and storing this information in psql database. I need help converting the returned decimal to ASCII characters. The following is an example of information returned from an 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 define various 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 $vs variable is the name of the object 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
source share
5 answers
 my $new_vs = join("", map { chr($_) } split(/\./,$vs)); 
0
source

Jonathan Leffler has the correct answer, but here are a few things to expand Perl's 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; 

Done to print:

 Version 1: forward_vs Version 2: forward_vs 

Once it becomes clear to you, you can press the spacebar to continue, or q to exit. :)

EDIT: The latter can also be written

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

But please don’t. :)

+1
source

A simple solution:

 $ascii .= chr for split /\./, $vs; 
0
source
 pack 'C*', split /\./ 

For instance,

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

Source: https://habr.com/ru/post/920991/


All Articles