Search code examples
perlperl-modulemod-perl

unable to convert string to hex in PERL


I am parsing a file which consists of decimal as well as hexadecimal values separated by ":":

foreach $line (<INFO>)  { 
    my ($seq_no, $size_in_bytes, $Hitcount, $buffer) = split /:/, $line;

    # $size in_bytes is a hexadecimal value.

    print "check 1 $size_in_bytes\n";      # printing some value in hexadecimal
    $size_in_bytes = hex($size_in_bytes);
    print "check 2  $size_in_bytes\n";     # Printing ZERO??
}

I tried below approach also but still it is giving ZERO only.

$dec_num = sprintf("%d", hex($num));

Can you please tell me how can I convert string to Decimal


Solution

  • Since the problem is with superfluous spaces in your fields, you should split like this instead

    split /\s*:\s*/, $line
    

    That way the spaces will be removed if there are any, but the split will still work fine if not.