简体   繁体   English

无法在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 你能告诉我如何将字符串转换为十进制吗

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. 这样,如果有空格,空格将被删除,但是如果没有空格, split仍然可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM